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