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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 17 | #include "clang/Parse/DeclSpec.h" |
| 18 | #include "clang/Parse/Scope.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // C99 6.8: Statements and Blocks. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 26 | /// StatementOrDeclaration: |
| 27 | /// statement |
| 28 | /// declaration |
| 29 | /// |
| 30 | /// statement: |
| 31 | /// labeled-statement |
| 32 | /// compound-statement |
| 33 | /// expression-statement |
| 34 | /// selection-statement |
| 35 | /// iteration-statement |
| 36 | /// jump-statement |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame^] | 37 | /// [OBC] objc-throw-statement |
| 38 | /// [OBC] objc-try-catch-statement |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | /// [OBC] objc-synchronized-statement [TODO] |
| 40 | /// [GNU] asm-statement |
| 41 | /// [OMP] openmp-construct [TODO] |
| 42 | /// |
| 43 | /// labeled-statement: |
| 44 | /// identifier ':' statement |
| 45 | /// 'case' constant-expression ':' statement |
| 46 | /// 'default' ':' statement |
| 47 | /// |
| 48 | /// selection-statement: |
| 49 | /// if-statement |
| 50 | /// switch-statement |
| 51 | /// |
| 52 | /// iteration-statement: |
| 53 | /// while-statement |
| 54 | /// do-statement |
| 55 | /// for-statement |
| 56 | /// |
| 57 | /// expression-statement: |
| 58 | /// expression[opt] ';' |
| 59 | /// |
| 60 | /// jump-statement: |
| 61 | /// 'goto' identifier ';' |
| 62 | /// 'continue' ';' |
| 63 | /// 'break' ';' |
| 64 | /// 'return' expression[opt] ';' |
| 65 | /// [GNU] 'goto' '*' expression ';' |
| 66 | /// |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame^] | 67 | /// [OBC] objc-throw-statement: |
| 68 | /// [OBC] '@' 'throw' expression ';' |
| 69 | /// [OBC] '@' 'throw' ';' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | /// |
| 71 | Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) { |
| 72 | const char *SemiError = 0; |
| 73 | Parser::StmtResult Res; |
| 74 | |
| 75 | // Cases in this switch statement should fall through if the parser expects |
| 76 | // the token to end in a semicolon (in which case SemiError should be set), |
| 77 | // or they directly 'return;' if not. |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 78 | tok::TokenKind Kind = Tok.getKind(); |
| 79 | SourceLocation AtLoc; |
| 80 | switch (Kind) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | case tok::identifier: // C99 6.8.1: labeled-statement |
| 82 | // identifier ':' statement |
| 83 | // declaration (if !OnlyStatement) |
| 84 | // expression[opt] ';' |
| 85 | return ParseIdentifierStatement(OnlyStatement); |
| 86 | |
Fariborz Jahanian | 397fcc1 | 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 @ |
| 90 | if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_try) |
| 91 | return ParseObjCTryStmt(AtLoc); |
| 92 | else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_throw) |
| 93 | return ParseObjCThrowStmt(AtLoc); |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame^] | 94 | ExprResult Res = ParseExpressionWithLeadingAt(AtLoc); |
| 95 | if (Res.isInvalid) { |
| 96 | // If the expression is invalid, skip ahead to the next semicolon. Not |
| 97 | // doing this opens us up to the possibility of infinite loops if |
| 98 | // ParseExpression does not consume any tokens. |
| 99 | SkipUntil(tok::semi); |
| 100 | return true; |
| 101 | } |
| 102 | // Otherwise, eat the semicolon. |
| 103 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
| 104 | return Actions.ActOnExprStmt(Res.Val); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 105 | } |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | default: |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame^] | 108 | if (!OnlyStatement && isDeclarationSpecifier()) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 109 | return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | } else if (Tok.getKind() == tok::r_brace) { |
| 111 | Diag(Tok, diag::err_expected_statement); |
| 112 | return true; |
| 113 | } else { |
| 114 | // expression[opt] ';' |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame^] | 115 | ExprResult Res = ParseExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | if (Res.isInvalid) { |
| 117 | // If the expression is invalid, skip ahead to the next semicolon. Not |
| 118 | // doing this opens us up to the possibility of infinite loops if |
| 119 | // ParseExpression does not consume any tokens. |
| 120 | SkipUntil(tok::semi); |
| 121 | return true; |
| 122 | } |
| 123 | // Otherwise, eat the semicolon. |
| 124 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 125 | return Actions.ActOnExprStmt(Res.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | case tok::kw_case: // C99 6.8.1: labeled-statement |
| 129 | return ParseCaseStatement(); |
| 130 | case tok::kw_default: // C99 6.8.1: labeled-statement |
| 131 | return ParseDefaultStatement(); |
| 132 | |
| 133 | case tok::l_brace: // C99 6.8.2: compound-statement |
| 134 | return ParseCompoundStatement(); |
| 135 | case tok::semi: // C99 6.8.3p3: expression[opt] ';' |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 136 | return Actions.ActOnNullStmt(ConsumeToken()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | |
| 138 | case tok::kw_if: // C99 6.8.4.1: if-statement |
| 139 | return ParseIfStatement(); |
| 140 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
| 141 | return ParseSwitchStatement(); |
| 142 | |
| 143 | case tok::kw_while: // C99 6.8.5.1: while-statement |
| 144 | return ParseWhileStatement(); |
| 145 | case tok::kw_do: // C99 6.8.5.2: do-statement |
| 146 | Res = ParseDoStatement(); |
| 147 | SemiError = "do/while loop"; |
| 148 | break; |
| 149 | case tok::kw_for: // C99 6.8.5.3: for-statement |
| 150 | return ParseForStatement(); |
| 151 | |
| 152 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
| 153 | Res = ParseGotoStatement(); |
| 154 | SemiError = "goto statement"; |
| 155 | break; |
| 156 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
| 157 | Res = ParseContinueStatement(); |
| 158 | SemiError = "continue statement"; |
| 159 | break; |
| 160 | case tok::kw_break: // C99 6.8.6.3: break-statement |
| 161 | Res = ParseBreakStatement(); |
| 162 | SemiError = "break statement"; |
| 163 | break; |
| 164 | case tok::kw_return: // C99 6.8.6.4: return-statement |
| 165 | Res = ParseReturnStatement(); |
| 166 | SemiError = "return statement"; |
| 167 | break; |
| 168 | |
| 169 | case tok::kw_asm: |
| 170 | Res = ParseAsmStatement(); |
| 171 | SemiError = "asm statement"; |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | // If we reached this code, the statement must end in a semicolon. |
| 176 | if (Tok.getKind() == tok::semi) { |
| 177 | ConsumeToken(); |
| 178 | } else { |
| 179 | Diag(Tok, diag::err_expected_semi_after, SemiError); |
| 180 | SkipUntil(tok::semi); |
| 181 | } |
| 182 | return Res; |
| 183 | } |
| 184 | |
| 185 | /// ParseIdentifierStatement - Because we don't have two-token lookahead, we |
| 186 | /// have a bit of a quandry here. Reading the identifier is necessary to see if |
| 187 | /// there is a ':' after it. If there is, this is a label, regardless of what |
| 188 | /// else the identifier can mean. If not, this is either part of a declaration |
| 189 | /// (if the identifier is a type-name) or part of an expression. |
| 190 | /// |
| 191 | /// labeled-statement: |
| 192 | /// identifier ':' statement |
| 193 | /// [GNU] identifier ':' attributes[opt] statement |
| 194 | /// declaration (if !OnlyStatement) |
| 195 | /// expression[opt] ';' |
| 196 | /// |
| 197 | Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) { |
| 198 | assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() && |
| 199 | "Not an identifier!"); |
| 200 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 201 | Token IdentTok = Tok; // Save the whole token. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | ConsumeToken(); // eat the identifier. |
| 203 | |
| 204 | // identifier ':' statement |
| 205 | if (Tok.getKind() == tok::colon) { |
| 206 | SourceLocation ColonLoc = ConsumeToken(); |
| 207 | |
| 208 | // Read label attributes, if present. |
| 209 | DeclTy *AttrList = 0; |
| 210 | if (Tok.getKind() == tok::kw___attribute) |
| 211 | // TODO: save these somewhere. |
| 212 | AttrList = ParseAttributes(); |
| 213 | |
| 214 | StmtResult SubStmt = ParseStatement(); |
| 215 | |
| 216 | // Broken substmt shouldn't prevent the label from being added to the AST. |
| 217 | if (SubStmt.isInvalid) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 218 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 220 | return Actions.ActOnLabelStmt(IdentTok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | IdentTok.getIdentifierInfo(), |
| 222 | ColonLoc, SubStmt.Val); |
| 223 | } |
| 224 | |
| 225 | // Check to see if this is a declaration. |
| 226 | void *TypeRep; |
| 227 | if (!OnlyStatement && |
| 228 | (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) { |
| 229 | // Handle this. Warn/disable if in middle of block and !C99. |
| 230 | DeclSpec DS; |
| 231 | |
| 232 | // Add the typedef name to the start of the decl-specs. |
| 233 | const char *PrevSpec = 0; |
| 234 | int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, |
| 235 | IdentTok.getLocation(), PrevSpec, |
| 236 | TypeRep); |
| 237 | assert(!isInvalid && "First declspec can't be invalid!"); |
| 238 | |
| 239 | // ParseDeclarationSpecifiers will continue from there. |
| 240 | ParseDeclarationSpecifiers(DS); |
| 241 | |
| 242 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 243 | // declaration-specifiers init-declarator-list[opt] ';' |
| 244 | if (Tok.getKind() == tok::semi) { |
| 245 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 246 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 247 | |
| 248 | ConsumeToken(); |
| 249 | // FIXME: Return this as a type decl. |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | // Parse all the declarators. |
| 254 | Declarator DeclaratorInfo(DS, Declarator::BlockContext); |
| 255 | ParseDeclarator(DeclaratorInfo); |
| 256 | |
| 257 | DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 258 | return Decl ? Actions.ActOnDeclStmt(Decl) : 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Otherwise, this is an expression. Seed it with II and parse it. |
| 262 | ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok); |
| 263 | if (Res.isInvalid) { |
| 264 | SkipUntil(tok::semi); |
| 265 | return true; |
| 266 | } else if (Tok.getKind() != tok::semi) { |
| 267 | Diag(Tok, diag::err_expected_semi_after, "expression"); |
| 268 | SkipUntil(tok::semi); |
| 269 | return true; |
| 270 | } else { |
| 271 | ConsumeToken(); |
| 272 | // Convert expr to a stmt. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 273 | return Actions.ActOnExprStmt(Res.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /// ParseCaseStatement |
| 278 | /// labeled-statement: |
| 279 | /// 'case' constant-expression ':' statement |
| 280 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
| 281 | /// |
| 282 | /// Note that this does not parse the 'statement' at the end. |
| 283 | /// |
| 284 | Parser::StmtResult Parser::ParseCaseStatement() { |
| 285 | assert(Tok.getKind() == tok::kw_case && "Not a case stmt!"); |
| 286 | SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'. |
| 287 | |
| 288 | ExprResult LHS = ParseConstantExpression(); |
| 289 | if (LHS.isInvalid) { |
| 290 | SkipUntil(tok::colon); |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | // GNU case range extension. |
| 295 | SourceLocation DotDotDotLoc; |
| 296 | ExprTy *RHSVal = 0; |
| 297 | if (Tok.getKind() == tok::ellipsis) { |
| 298 | Diag(Tok, diag::ext_gnu_case_range); |
| 299 | DotDotDotLoc = ConsumeToken(); |
| 300 | |
| 301 | ExprResult RHS = ParseConstantExpression(); |
| 302 | if (RHS.isInvalid) { |
| 303 | SkipUntil(tok::colon); |
| 304 | return true; |
| 305 | } |
| 306 | RHSVal = RHS.Val; |
| 307 | } |
| 308 | |
| 309 | if (Tok.getKind() != tok::colon) { |
| 310 | Diag(Tok, diag::err_expected_colon_after, "'case'"); |
| 311 | SkipUntil(tok::colon); |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | SourceLocation ColonLoc = ConsumeToken(); |
| 316 | |
| 317 | // Diagnose the common error "switch (X) { case 4: }", which is not valid. |
| 318 | if (Tok.getKind() == tok::r_brace) { |
| 319 | Diag(Tok, diag::err_label_end_of_compound_statement); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | StmtResult SubStmt = ParseStatement(); |
| 324 | |
| 325 | // Broken substmt shouldn't prevent the case from being added to the AST. |
| 326 | if (SubStmt.isInvalid) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 327 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 329 | return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | SubStmt.Val); |
| 331 | } |
| 332 | |
| 333 | /// ParseDefaultStatement |
| 334 | /// labeled-statement: |
| 335 | /// 'default' ':' statement |
| 336 | /// Note that this does not parse the 'statement' at the end. |
| 337 | /// |
| 338 | Parser::StmtResult Parser::ParseDefaultStatement() { |
| 339 | assert(Tok.getKind() == tok::kw_default && "Not a default stmt!"); |
| 340 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
| 341 | |
| 342 | if (Tok.getKind() != tok::colon) { |
| 343 | Diag(Tok, diag::err_expected_colon_after, "'default'"); |
| 344 | SkipUntil(tok::colon); |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | SourceLocation ColonLoc = ConsumeToken(); |
| 349 | |
| 350 | // Diagnose the common error "switch (X) {... default: }", which is not valid. |
| 351 | if (Tok.getKind() == tok::r_brace) { |
| 352 | Diag(Tok, diag::err_label_end_of_compound_statement); |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | StmtResult SubStmt = ParseStatement(); |
| 357 | if (SubStmt.isInvalid) |
| 358 | return true; |
| 359 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 360 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | /// ParseCompoundStatement - Parse a "{}" block. |
| 365 | /// |
| 366 | /// compound-statement: [C99 6.8.2] |
| 367 | /// { block-item-list[opt] } |
| 368 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 369 | /// |
| 370 | /// block-item-list: |
| 371 | /// block-item |
| 372 | /// block-item-list block-item |
| 373 | /// |
| 374 | /// block-item: |
| 375 | /// declaration |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 376 | /// [GNU] '__extension__' declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | /// statement |
| 378 | /// [OMP] openmp-directive [TODO] |
| 379 | /// |
| 380 | /// [GNU] label-declarations: |
| 381 | /// [GNU] label-declaration |
| 382 | /// [GNU] label-declarations label-declaration |
| 383 | /// |
| 384 | /// [GNU] label-declaration: |
| 385 | /// [GNU] '__label__' identifier-list ';' |
| 386 | /// |
| 387 | /// [OMP] openmp-directive: [TODO] |
| 388 | /// [OMP] barrier-directive |
| 389 | /// [OMP] flush-directive |
| 390 | /// |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 391 | Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 392 | assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!"); |
| 393 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 394 | // Enter a scope to hold everything within the compound stmt. Compound |
| 395 | // statements can always hold declarations. |
| 396 | EnterScope(Scope::DeclScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | |
| 398 | // Parse the statements in the body. |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 399 | StmtResult Body = ParseCompoundStatementBody(isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | |
| 401 | ExitScope(); |
| 402 | return Body; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 407 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 409 | /// stack. |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 410 | Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'. |
| 412 | |
| 413 | // 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] | 414 | // 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] | 415 | |
| 416 | llvm::SmallVector<StmtTy*, 32> Stmts; |
| 417 | while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) { |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 418 | StmtResult R; |
| 419 | if (Tok.getKind() != tok::kw___extension__) { |
| 420 | R = ParseStatementOrDeclaration(false); |
| 421 | } else { |
| 422 | // __extension__ can start declarations and it can also be a unary |
| 423 | // operator for expressions. Consume multiple __extension__ markers here |
| 424 | // until we can determine which is which. |
| 425 | SourceLocation ExtLoc = ConsumeToken(); |
| 426 | while (Tok.getKind() == tok::kw___extension__) |
| 427 | ConsumeToken(); |
| 428 | |
| 429 | // If this is the start of a declaration, parse it as such. |
| 430 | if (isDeclarationSpecifier()) { |
| 431 | // FIXME: Save the __extension__ on the decl as a node somehow. |
| 432 | // FIXME: disable extwarns. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 433 | R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext)); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 434 | } else { |
| 435 | // Otherwise this was a unary __extension__ marker. Parse the |
| 436 | // subexpression and add the __extension__ unary op. |
| 437 | // FIXME: disable extwarns. |
| 438 | ExprResult Res = ParseCastExpression(false); |
| 439 | if (Res.isInvalid) { |
| 440 | SkipUntil(tok::semi); |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | // Add the __extension__ node to the AST. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 445 | Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 446 | if (Res.isInvalid) |
| 447 | continue; |
| 448 | |
| 449 | // Eat the semicolon at the end of stmt and convert the expr into a stmt. |
| 450 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 451 | R = Actions.ActOnExprStmt(Res.Val); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 455 | if (!R.isInvalid && R.Val) |
| 456 | Stmts.push_back(R.Val); |
| 457 | } |
| 458 | |
| 459 | // We broke out of the while loop because we found a '}' or EOF. |
| 460 | if (Tok.getKind() != tok::r_brace) { |
| 461 | Diag(Tok, diag::err_expected_rbrace); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | SourceLocation RBraceLoc = ConsumeBrace(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 466 | return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 467 | &Stmts[0], Stmts.size(), isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /// ParseIfStatement |
| 471 | /// if-statement: [C99 6.8.4.1] |
| 472 | /// 'if' '(' expression ')' statement |
| 473 | /// 'if' '(' expression ')' statement 'else' statement |
| 474 | /// |
| 475 | Parser::StmtResult Parser::ParseIfStatement() { |
| 476 | assert(Tok.getKind() == tok::kw_if && "Not an if stmt!"); |
| 477 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
| 478 | |
| 479 | if (Tok.getKind() != tok::l_paren) { |
| 480 | Diag(Tok, diag::err_expected_lparen_after, "if"); |
| 481 | SkipUntil(tok::semi); |
| 482 | return true; |
| 483 | } |
| 484 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 485 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 486 | // the case for C90. |
| 487 | if (getLang().C99) |
| 488 | EnterScope(Scope::DeclScope); |
| 489 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | // Parse the condition. |
| 491 | ExprResult CondExp = ParseSimpleParenExpression(); |
| 492 | if (CondExp.isInvalid) { |
| 493 | SkipUntil(tok::semi); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 494 | if (getLang().C99) |
| 495 | ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 499 | // 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] | 500 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 501 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 502 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 503 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 505 | // Read the if condition. |
| 506 | StmtResult CondStmt = ParseStatement(); |
| 507 | |
| 508 | // Broken substmt shouldn't prevent the label from being added to the AST. |
| 509 | if (CondStmt.isInvalid) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 510 | CondStmt = Actions.ActOnNullStmt(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 512 | // Pop the 'if' scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 513 | if (NeedsInnerScope) ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | |
| 515 | // If it has an else, parse it. |
| 516 | SourceLocation ElseLoc; |
| 517 | StmtResult ElseStmt(false); |
| 518 | if (Tok.getKind() == tok::kw_else) { |
| 519 | ElseLoc = ConsumeToken(); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 521 | // 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] | 522 | // there is no compound stmt. C90 does not have this clause. We only do |
| 523 | // this if the body isn't a compound statement to avoid push/pop in common |
| 524 | // cases. |
| 525 | NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 526 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 527 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | ElseStmt = ParseStatement(); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 529 | |
| 530 | // Pop the 'else' scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 531 | if (NeedsInnerScope) ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | |
| 533 | if (ElseStmt.isInvalid) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 534 | ElseStmt = Actions.ActOnNullStmt(ElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 537 | if (getLang().C99) |
| 538 | ExitScope(); |
| 539 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 540 | return Actions.ActOnIfStmt(IfLoc, CondExp.Val, CondStmt.Val, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | ElseLoc, ElseStmt.Val); |
| 542 | } |
| 543 | |
| 544 | /// ParseSwitchStatement |
| 545 | /// switch-statement: |
| 546 | /// 'switch' '(' expression ')' statement |
| 547 | Parser::StmtResult Parser::ParseSwitchStatement() { |
| 548 | assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!"); |
| 549 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
| 550 | |
| 551 | if (Tok.getKind() != tok::l_paren) { |
| 552 | Diag(Tok, diag::err_expected_lparen_after, "switch"); |
| 553 | SkipUntil(tok::semi); |
| 554 | return true; |
| 555 | } |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 556 | |
| 557 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 558 | // not the case for C90. Start the switch scope. |
| 559 | if (getLang().C99) |
| 560 | EnterScope(Scope::BreakScope|Scope::DeclScope); |
| 561 | else |
| 562 | EnterScope(Scope::BreakScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | |
| 564 | // Parse the condition. |
| 565 | ExprResult Cond = ParseSimpleParenExpression(); |
| 566 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 567 | if (Cond.isInvalid) { |
| 568 | ExitScope(); |
| 569 | return true; |
| 570 | } |
| 571 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 572 | StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 574 | // 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] | 575 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 576 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 577 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 578 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 579 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 580 | // Read the body statement. |
| 581 | StmtResult Body = ParseStatement(); |
| 582 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 583 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 584 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 585 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 586 | if (Body.isInvalid) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 587 | Body = Actions.ActOnNullStmt(Tok.getLocation()); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 588 | // FIXME: Remove the case statement list from the Switch statement. |
| 589 | } |
| 590 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | ExitScope(); |
| 592 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 593 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /// ParseWhileStatement |
| 597 | /// while-statement: [C99 6.8.5.1] |
| 598 | /// 'while' '(' expression ')' statement |
| 599 | Parser::StmtResult Parser::ParseWhileStatement() { |
| 600 | assert(Tok.getKind() == tok::kw_while && "Not a while stmt!"); |
| 601 | SourceLocation WhileLoc = Tok.getLocation(); |
| 602 | ConsumeToken(); // eat the 'while'. |
| 603 | |
| 604 | if (Tok.getKind() != tok::l_paren) { |
| 605 | Diag(Tok, diag::err_expected_lparen_after, "while"); |
| 606 | SkipUntil(tok::semi); |
| 607 | return true; |
| 608 | } |
| 609 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 610 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 611 | // the case for C90. Start the loop scope. |
| 612 | if (getLang().C99) |
| 613 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 614 | else |
| 615 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | |
| 617 | // Parse the condition. |
| 618 | ExprResult Cond = ParseSimpleParenExpression(); |
| 619 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 620 | // 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] | 621 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 622 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 623 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 624 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 625 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 626 | // Read the body statement. |
| 627 | StmtResult Body = ParseStatement(); |
| 628 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 629 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 630 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 631 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | ExitScope(); |
| 633 | |
| 634 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 635 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 636 | return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /// ParseDoStatement |
| 640 | /// do-statement: [C99 6.8.5.2] |
| 641 | /// 'do' statement 'while' '(' expression ')' ';' |
| 642 | /// Note: this lets the caller parse the end ';'. |
| 643 | Parser::StmtResult Parser::ParseDoStatement() { |
| 644 | assert(Tok.getKind() == tok::kw_do && "Not a do stmt!"); |
| 645 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
| 646 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 647 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 648 | // the case for C90. Start the loop scope. |
| 649 | if (getLang().C99) |
| 650 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 651 | else |
| 652 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 654 | // 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] | 655 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 656 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 657 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 658 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 660 | // Read the body statement. |
| 661 | StmtResult Body = ParseStatement(); |
| 662 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 663 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 664 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 665 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 666 | if (Tok.getKind() != tok::kw_while) { |
| 667 | ExitScope(); |
| 668 | Diag(Tok, diag::err_expected_while); |
| 669 | Diag(DoLoc, diag::err_matching, "do"); |
| 670 | SkipUntil(tok::semi); |
| 671 | return true; |
| 672 | } |
| 673 | SourceLocation WhileLoc = ConsumeToken(); |
| 674 | |
| 675 | if (Tok.getKind() != tok::l_paren) { |
| 676 | ExitScope(); |
| 677 | Diag(Tok, diag::err_expected_lparen_after, "do/while"); |
| 678 | SkipUntil(tok::semi); |
| 679 | return true; |
| 680 | } |
| 681 | |
| 682 | // Parse the condition. |
| 683 | ExprResult Cond = ParseSimpleParenExpression(); |
| 684 | |
| 685 | ExitScope(); |
| 686 | |
| 687 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 688 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 689 | return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /// ParseForStatement |
| 693 | /// for-statement: [C99 6.8.5.3] |
| 694 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 695 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
| 696 | Parser::StmtResult Parser::ParseForStatement() { |
| 697 | assert(Tok.getKind() == tok::kw_for && "Not a for stmt!"); |
| 698 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
| 699 | |
| 700 | if (Tok.getKind() != tok::l_paren) { |
| 701 | Diag(Tok, diag::err_expected_lparen_after, "for"); |
| 702 | SkipUntil(tok::semi); |
| 703 | return true; |
| 704 | } |
| 705 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 706 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 707 | // the case for C90. Start the loop scope. |
| 708 | if (getLang().C99) |
| 709 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 710 | else |
| 711 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | |
| 713 | SourceLocation LParenLoc = ConsumeParen(); |
| 714 | ExprResult Value; |
| 715 | |
| 716 | StmtTy *FirstPart = 0; |
| 717 | ExprTy *SecondPart = 0; |
| 718 | StmtTy *ThirdPart = 0; |
| 719 | |
| 720 | // Parse the first part of the for specifier. |
| 721 | if (Tok.getKind() == tok::semi) { // for (; |
| 722 | // no first part, eat the ';'. |
| 723 | ConsumeToken(); |
| 724 | } else if (isDeclarationSpecifier()) { // for (int X = 4; |
| 725 | // Parse declaration, which eats the ';'. |
| 726 | if (!getLang().C99) // Use of C99-style for loops in C90 mode? |
| 727 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
| 728 | DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 729 | StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 730 | FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val; |
| 731 | } else { |
| 732 | Value = ParseExpression(); |
| 733 | |
| 734 | // Turn the expression into a stmt. |
| 735 | if (!Value.isInvalid) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 736 | StmtResult R = Actions.ActOnExprStmt(Value.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | if (!R.isInvalid) |
| 738 | FirstPart = R.Val; |
| 739 | } |
| 740 | |
| 741 | if (Tok.getKind() == tok::semi) { |
| 742 | ConsumeToken(); |
| 743 | } else { |
| 744 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 745 | SkipUntil(tok::semi); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // Parse the second part of the for specifier. |
| 750 | if (Tok.getKind() == tok::semi) { // for (...;; |
| 751 | // no second part. |
| 752 | Value = ExprResult(); |
| 753 | } else { |
| 754 | Value = ParseExpression(); |
| 755 | if (!Value.isInvalid) |
| 756 | SecondPart = Value.Val; |
| 757 | } |
| 758 | |
| 759 | if (Tok.getKind() == tok::semi) { |
| 760 | ConsumeToken(); |
| 761 | } else { |
| 762 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 763 | SkipUntil(tok::semi); |
| 764 | } |
| 765 | |
| 766 | // Parse the third part of the for specifier. |
| 767 | if (Tok.getKind() == tok::r_paren) { // for (...;...;) |
| 768 | // no third part. |
| 769 | Value = ExprResult(); |
| 770 | } else { |
| 771 | Value = ParseExpression(); |
| 772 | if (!Value.isInvalid) { |
| 773 | // Turn the expression into a stmt. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 774 | StmtResult R = Actions.ActOnExprStmt(Value.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 775 | if (!R.isInvalid) |
| 776 | ThirdPart = R.Val; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // Match the ')'. |
| 781 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 782 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 783 | // 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] | 784 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 785 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 786 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 787 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 788 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 789 | // Read the body statement. |
| 790 | StmtResult Body = ParseStatement(); |
| 791 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 792 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 793 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 794 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | // Leave the for-scope. |
| 796 | ExitScope(); |
| 797 | |
| 798 | if (Body.isInvalid) |
| 799 | return Body; |
| 800 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 801 | return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart, SecondPart, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 802 | ThirdPart, RParenLoc, Body.Val); |
| 803 | } |
| 804 | |
| 805 | /// ParseGotoStatement |
| 806 | /// jump-statement: |
| 807 | /// 'goto' identifier ';' |
| 808 | /// [GNU] 'goto' '*' expression ';' |
| 809 | /// |
| 810 | /// Note: this lets the caller parse the end ';'. |
| 811 | /// |
| 812 | Parser::StmtResult Parser::ParseGotoStatement() { |
| 813 | assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!"); |
| 814 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
| 815 | |
| 816 | StmtResult Res; |
| 817 | if (Tok.getKind() == tok::identifier) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 818 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | Tok.getIdentifierInfo()); |
| 820 | ConsumeToken(); |
| 821 | } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) { |
| 822 | // GNU indirect goto extension. |
| 823 | Diag(Tok, diag::ext_gnu_indirect_goto); |
| 824 | SourceLocation StarLoc = ConsumeToken(); |
| 825 | ExprResult R = ParseExpression(); |
| 826 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 827 | SkipUntil(tok::semi, false, true); |
| 828 | return true; |
| 829 | } |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 830 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val); |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 831 | } else { |
| 832 | Diag(Tok, diag::err_expected_ident); |
| 833 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 834 | } |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | return Res; |
| 837 | } |
| 838 | |
| 839 | /// ParseContinueStatement |
| 840 | /// jump-statement: |
| 841 | /// 'continue' ';' |
| 842 | /// |
| 843 | /// Note: this lets the caller parse the end ';'. |
| 844 | /// |
| 845 | Parser::StmtResult Parser::ParseContinueStatement() { |
| 846 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 847 | return Actions.ActOnContinueStmt(ContinueLoc, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /// ParseBreakStatement |
| 851 | /// jump-statement: |
| 852 | /// 'break' ';' |
| 853 | /// |
| 854 | /// Note: this lets the caller parse the end ';'. |
| 855 | /// |
| 856 | Parser::StmtResult Parser::ParseBreakStatement() { |
| 857 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 858 | return Actions.ActOnBreakStmt(BreakLoc, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /// ParseReturnStatement |
| 862 | /// jump-statement: |
| 863 | /// 'return' expression[opt] ';' |
| 864 | Parser::StmtResult Parser::ParseReturnStatement() { |
| 865 | assert(Tok.getKind() == tok::kw_return && "Not a return stmt!"); |
| 866 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
| 867 | |
| 868 | ExprResult R(0); |
| 869 | if (Tok.getKind() != tok::semi) { |
| 870 | R = ParseExpression(); |
| 871 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 872 | SkipUntil(tok::semi, false, true); |
| 873 | return true; |
| 874 | } |
| 875 | } |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 876 | return Actions.ActOnReturnStmt(ReturnLoc, R.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
| 880 | /// [GNU] asm-statement: |
| 881 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 882 | /// |
| 883 | /// [GNU] asm-argument: |
| 884 | /// asm-string-literal |
| 885 | /// asm-string-literal ':' asm-operands[opt] |
| 886 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 887 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 888 | /// ':' asm-clobbers |
| 889 | /// |
| 890 | /// [GNU] asm-clobbers: |
| 891 | /// asm-string-literal |
| 892 | /// asm-clobbers ',' asm-string-literal |
| 893 | /// |
| 894 | Parser::StmtResult Parser::ParseAsmStatement() { |
| 895 | assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt"); |
| 896 | ConsumeToken(); |
| 897 | |
| 898 | DeclSpec DS; |
| 899 | SourceLocation Loc = Tok.getLocation(); |
| 900 | ParseTypeQualifierListOpt(DS); |
| 901 | |
| 902 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
| 903 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 904 | Diag(Loc, diag::w_asm_qualifier_ignored, "const"); |
| 905 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| 906 | Diag(Loc, diag::w_asm_qualifier_ignored, "restrict"); |
| 907 | |
| 908 | // Remember if this was a volatile asm. |
| 909 | //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile; |
| 910 | |
| 911 | if (Tok.getKind() != tok::l_paren) { |
| 912 | Diag(Tok, diag::err_expected_lparen_after, "asm"); |
| 913 | SkipUntil(tok::r_paren); |
| 914 | return true; |
| 915 | } |
| 916 | Loc = ConsumeParen(); |
| 917 | |
| 918 | ParseAsmStringLiteral(); |
| 919 | |
| 920 | // Parse Outputs, if present. |
| 921 | ParseAsmOperandsOpt(); |
| 922 | |
| 923 | // Parse Inputs, if present. |
| 924 | ParseAsmOperandsOpt(); |
| 925 | |
| 926 | // Parse the clobbers, if present. |
| 927 | if (Tok.getKind() == tok::colon) { |
| 928 | ConsumeToken(); |
| 929 | |
| 930 | if (isTokenStringLiteral()) { |
| 931 | // Parse the asm-string list for clobbers. |
| 932 | while (1) { |
| 933 | ParseAsmStringLiteral(); |
| 934 | |
| 935 | if (Tok.getKind() != tok::comma) break; |
| 936 | ConsumeToken(); |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | MatchRHSPunctuation(tok::r_paren, Loc); |
| 942 | |
| 943 | // FIXME: Implement action for asm parsing. |
| 944 | return false; |
| 945 | } |
| 946 | |
| 947 | /// ParseAsmOperands - Parse the asm-operands production as used by |
| 948 | /// asm-statement. We also parse a leading ':' token. If the leading colon is |
| 949 | /// not present, we do not parse anything. |
| 950 | /// |
| 951 | /// [GNU] asm-operands: |
| 952 | /// asm-operand |
| 953 | /// asm-operands ',' asm-operand |
| 954 | /// |
| 955 | /// [GNU] asm-operand: |
| 956 | /// asm-string-literal '(' expression ')' |
| 957 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 958 | /// |
| 959 | void Parser::ParseAsmOperandsOpt() { |
| 960 | // Only do anything if this operand is present. |
| 961 | if (Tok.getKind() != tok::colon) return; |
| 962 | ConsumeToken(); |
| 963 | |
| 964 | // 'asm-operands' isn't present? |
| 965 | if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square) |
| 966 | return; |
| 967 | |
| 968 | while (1) { |
| 969 | // Read the [id] if present. |
| 970 | if (Tok.getKind() == tok::l_square) { |
| 971 | SourceLocation Loc = ConsumeBracket(); |
| 972 | |
| 973 | if (Tok.getKind() != tok::identifier) { |
| 974 | Diag(Tok, diag::err_expected_ident); |
| 975 | SkipUntil(tok::r_paren); |
| 976 | return; |
| 977 | } |
| 978 | MatchRHSPunctuation(tok::r_square, Loc); |
| 979 | } |
| 980 | |
| 981 | ParseAsmStringLiteral(); |
| 982 | |
| 983 | if (Tok.getKind() != tok::l_paren) { |
| 984 | Diag(Tok, diag::err_expected_lparen_after, "asm operand"); |
| 985 | SkipUntil(tok::r_paren); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | // Read the parenthesized expression. |
| 990 | ExprResult Res = ParseSimpleParenExpression(); |
| 991 | if (Res.isInvalid) { |
| 992 | SkipUntil(tok::r_paren); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | // Eat the comma and continue parsing if it exists. |
| 997 | if (Tok.getKind() != tok::comma) return; |
| 998 | ConsumeToken(); |
| 999 | } |
| 1000 | } |