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)); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 110 | } else if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 176 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 198 | assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 205 | if (Tok.is(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | SourceLocation ColonLoc = ConsumeToken(); |
| 207 | |
| 208 | // Read label attributes, if present. |
| 209 | DeclTy *AttrList = 0; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 210 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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!"); |
Fariborz Jahanian | dfbcce2 | 2007-10-11 18:08:47 +0000 | [diff] [blame] | 238 | if (Tok.is(tok::less)) { |
| 239 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
| 240 | ParseObjCProtocolReferences(ProtocolRefs); |
| 241 | llvm::SmallVector<DeclTy *, 8> *ProtocolDecl = |
| 242 | new llvm::SmallVector<DeclTy *, 8>; |
| 243 | DS.setProtocolQualifiers(ProtocolDecl); |
| 244 | Actions.FindProtocolDeclaration(IdentTok.getLocation(), |
| 245 | &ProtocolRefs[0], ProtocolRefs.size(), |
| 246 | *ProtocolDecl); |
| 247 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | |
| 249 | // ParseDeclarationSpecifiers will continue from there. |
| 250 | ParseDeclarationSpecifiers(DS); |
| 251 | |
| 252 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 253 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 254 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 256 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 257 | |
| 258 | ConsumeToken(); |
| 259 | // FIXME: Return this as a type decl. |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | // Parse all the declarators. |
| 264 | Declarator DeclaratorInfo(DS, Declarator::BlockContext); |
| 265 | ParseDeclarator(DeclaratorInfo); |
| 266 | |
| 267 | DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 268 | return Decl ? Actions.ActOnDeclStmt(Decl) : 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // Otherwise, this is an expression. Seed it with II and parse it. |
| 272 | ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok); |
| 273 | if (Res.isInvalid) { |
| 274 | SkipUntil(tok::semi); |
| 275 | return true; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 276 | } else if (Tok.isNot(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | Diag(Tok, diag::err_expected_semi_after, "expression"); |
| 278 | SkipUntil(tok::semi); |
| 279 | return true; |
| 280 | } else { |
| 281 | ConsumeToken(); |
| 282 | // Convert expr to a stmt. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 283 | return Actions.ActOnExprStmt(Res.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | /// ParseCaseStatement |
| 288 | /// labeled-statement: |
| 289 | /// 'case' constant-expression ':' statement |
| 290 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
| 291 | /// |
| 292 | /// Note that this does not parse the 'statement' at the end. |
| 293 | /// |
| 294 | Parser::StmtResult Parser::ParseCaseStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 295 | assert(Tok.is(tok::kw_case) && "Not a case stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'. |
| 297 | |
| 298 | ExprResult LHS = ParseConstantExpression(); |
| 299 | if (LHS.isInvalid) { |
| 300 | SkipUntil(tok::colon); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | // GNU case range extension. |
| 305 | SourceLocation DotDotDotLoc; |
| 306 | ExprTy *RHSVal = 0; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 307 | if (Tok.is(tok::ellipsis)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | Diag(Tok, diag::ext_gnu_case_range); |
| 309 | DotDotDotLoc = ConsumeToken(); |
| 310 | |
| 311 | ExprResult RHS = ParseConstantExpression(); |
| 312 | if (RHS.isInvalid) { |
| 313 | SkipUntil(tok::colon); |
| 314 | return true; |
| 315 | } |
| 316 | RHSVal = RHS.Val; |
| 317 | } |
| 318 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 319 | if (Tok.isNot(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | Diag(Tok, diag::err_expected_colon_after, "'case'"); |
| 321 | SkipUntil(tok::colon); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | SourceLocation ColonLoc = ConsumeToken(); |
| 326 | |
| 327 | // Diagnose the common error "switch (X) { case 4: }", which is not valid. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 328 | if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | Diag(Tok, diag::err_label_end_of_compound_statement); |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | StmtResult SubStmt = ParseStatement(); |
| 334 | |
| 335 | // Broken substmt shouldn't prevent the case from being added to the AST. |
| 336 | if (SubStmt.isInvalid) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 337 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 339 | return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | SubStmt.Val); |
| 341 | } |
| 342 | |
| 343 | /// ParseDefaultStatement |
| 344 | /// labeled-statement: |
| 345 | /// 'default' ':' statement |
| 346 | /// Note that this does not parse the 'statement' at the end. |
| 347 | /// |
| 348 | Parser::StmtResult Parser::ParseDefaultStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 349 | assert(Tok.is(tok::kw_default) && "Not a default stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
| 351 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 352 | if (Tok.isNot(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | Diag(Tok, diag::err_expected_colon_after, "'default'"); |
| 354 | SkipUntil(tok::colon); |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | SourceLocation ColonLoc = ConsumeToken(); |
| 359 | |
| 360 | // Diagnose the common error "switch (X) {... default: }", which is not valid. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 361 | if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | Diag(Tok, diag::err_label_end_of_compound_statement); |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | StmtResult SubStmt = ParseStatement(); |
| 367 | if (SubStmt.isInvalid) |
| 368 | return true; |
| 369 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 370 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
| 374 | /// ParseCompoundStatement - Parse a "{}" block. |
| 375 | /// |
| 376 | /// compound-statement: [C99 6.8.2] |
| 377 | /// { block-item-list[opt] } |
| 378 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 379 | /// |
| 380 | /// block-item-list: |
| 381 | /// block-item |
| 382 | /// block-item-list block-item |
| 383 | /// |
| 384 | /// block-item: |
| 385 | /// declaration |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 386 | /// [GNU] '__extension__' declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 387 | /// statement |
| 388 | /// [OMP] openmp-directive [TODO] |
| 389 | /// |
| 390 | /// [GNU] label-declarations: |
| 391 | /// [GNU] label-declaration |
| 392 | /// [GNU] label-declarations label-declaration |
| 393 | /// |
| 394 | /// [GNU] label-declaration: |
| 395 | /// [GNU] '__label__' identifier-list ';' |
| 396 | /// |
| 397 | /// [OMP] openmp-directive: [TODO] |
| 398 | /// [OMP] barrier-directive |
| 399 | /// [OMP] flush-directive |
| 400 | /// |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 401 | Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 402 | assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 404 | // Enter a scope to hold everything within the compound stmt. Compound |
| 405 | // statements can always hold declarations. |
| 406 | EnterScope(Scope::DeclScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | |
| 408 | // Parse the statements in the body. |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 409 | StmtResult Body = ParseCompoundStatementBody(isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | |
| 411 | ExitScope(); |
| 412 | return Body; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 417 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 418 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 419 | /// stack. |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 420 | Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'. |
| 422 | |
| 423 | // 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] | 424 | // 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] | 425 | |
| 426 | llvm::SmallVector<StmtTy*, 32> Stmts; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 427 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 428 | StmtResult R; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 429 | if (Tok.isNot(tok::kw___extension__)) { |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 430 | R = ParseStatementOrDeclaration(false); |
| 431 | } else { |
| 432 | // __extension__ can start declarations and it can also be a unary |
| 433 | // operator for expressions. Consume multiple __extension__ markers here |
| 434 | // until we can determine which is which. |
| 435 | SourceLocation ExtLoc = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 436 | while (Tok.is(tok::kw___extension__)) |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 437 | ConsumeToken(); |
| 438 | |
| 439 | // If this is the start of a declaration, parse it as such. |
| 440 | if (isDeclarationSpecifier()) { |
| 441 | // FIXME: Save the __extension__ on the decl as a node somehow. |
| 442 | // FIXME: disable extwarns. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 443 | R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext)); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 444 | } else { |
| 445 | // Otherwise this was a unary __extension__ marker. Parse the |
| 446 | // subexpression and add the __extension__ unary op. |
| 447 | // FIXME: disable extwarns. |
| 448 | ExprResult Res = ParseCastExpression(false); |
| 449 | if (Res.isInvalid) { |
| 450 | SkipUntil(tok::semi); |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | // Add the __extension__ node to the AST. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 455 | Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 456 | if (Res.isInvalid) |
| 457 | continue; |
| 458 | |
| 459 | // Eat the semicolon at the end of stmt and convert the expr into a stmt. |
| 460 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 461 | R = Actions.ActOnExprStmt(Res.Val); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 465 | if (!R.isInvalid && R.Val) |
| 466 | Stmts.push_back(R.Val); |
| 467 | } |
| 468 | |
| 469 | // 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] | 470 | if (Tok.isNot(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 471 | Diag(Tok, diag::err_expected_rbrace); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | SourceLocation RBraceLoc = ConsumeBrace(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 476 | return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 477 | &Stmts[0], Stmts.size(), isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /// ParseIfStatement |
| 481 | /// if-statement: [C99 6.8.4.1] |
| 482 | /// 'if' '(' expression ')' statement |
| 483 | /// 'if' '(' expression ')' statement 'else' statement |
| 484 | /// |
| 485 | Parser::StmtResult Parser::ParseIfStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 486 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
| 488 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 489 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | Diag(Tok, diag::err_expected_lparen_after, "if"); |
| 491 | SkipUntil(tok::semi); |
| 492 | return true; |
| 493 | } |
| 494 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 495 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 496 | // the case for C90. |
| 497 | if (getLang().C99) |
| 498 | EnterScope(Scope::DeclScope); |
| 499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | // Parse the condition. |
| 501 | ExprResult CondExp = ParseSimpleParenExpression(); |
| 502 | if (CondExp.isInvalid) { |
| 503 | SkipUntil(tok::semi); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 504 | if (getLang().C99) |
| 505 | ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | return true; |
| 507 | } |
| 508 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 509 | // 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] | 510 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 511 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 512 | bool NeedsInnerScope = getLang().C99 && 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 | // Read the 'then' stmt. |
| 516 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
| 517 | StmtResult ThenStmt = ParseStatement(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 519 | // Pop the 'if' scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 520 | if (NeedsInnerScope) ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 521 | |
| 522 | // If it has an else, parse it. |
| 523 | SourceLocation ElseLoc; |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame^] | 524 | SourceLocation ElseStmtLoc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 525 | StmtResult ElseStmt(false); |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame^] | 526 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 527 | if (Tok.is(tok::kw_else)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | ElseLoc = ConsumeToken(); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 530 | // 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] | 531 | // there is no compound stmt. C90 does not have this clause. We only do |
| 532 | // this if the body isn't a compound statement to avoid push/pop in common |
| 533 | // cases. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 534 | NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 535 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 536 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame^] | 537 | ElseStmtLoc = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | ElseStmt = ParseStatement(); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 539 | |
| 540 | // Pop the 'else' scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 541 | if (NeedsInnerScope) ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 544 | if (getLang().C99) |
| 545 | ExitScope(); |
| 546 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame^] | 547 | // If the then or else stmt is invalid and the other is valid (and present), |
| 548 | // make turn the invalid one into a null stmt to avoid dropping the other |
| 549 | // part. If both are invalid, return error. |
| 550 | if ((ThenStmt.isInvalid && ElseStmt.isInvalid) || |
| 551 | (ThenStmt.isInvalid && ElseStmt.Val == 0) || |
| 552 | (ThenStmt.Val == 0 && ElseStmt.isInvalid)) { |
| 553 | // Both invalid, or one is invalid and other is non-present: delete cond and |
| 554 | // return error. |
| 555 | Actions.DeleteExpr(CondExp.Val); |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | // Now if either are invalid, replace with a ';'. |
| 560 | if (ThenStmt.isInvalid) |
| 561 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
| 562 | if (ElseStmt.isInvalid) |
| 563 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
| 564 | |
| 565 | |
| 566 | |
| 567 | return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 568 | ElseLoc, ElseStmt.Val); |
| 569 | } |
| 570 | |
| 571 | /// ParseSwitchStatement |
| 572 | /// switch-statement: |
| 573 | /// 'switch' '(' expression ')' statement |
| 574 | Parser::StmtResult Parser::ParseSwitchStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 575 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 576 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
| 577 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 578 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 579 | Diag(Tok, diag::err_expected_lparen_after, "switch"); |
| 580 | SkipUntil(tok::semi); |
| 581 | return true; |
| 582 | } |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 583 | |
| 584 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 585 | // not the case for C90. Start the switch scope. |
| 586 | if (getLang().C99) |
| 587 | EnterScope(Scope::BreakScope|Scope::DeclScope); |
| 588 | else |
| 589 | EnterScope(Scope::BreakScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 590 | |
| 591 | // Parse the condition. |
| 592 | ExprResult Cond = ParseSimpleParenExpression(); |
| 593 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 594 | if (Cond.isInvalid) { |
| 595 | ExitScope(); |
| 596 | return true; |
| 597 | } |
| 598 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 599 | StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 601 | // 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] | 602 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 603 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 604 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 605 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 606 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 607 | // Read the body statement. |
| 608 | StmtResult Body = ParseStatement(); |
| 609 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 610 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 611 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 612 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 613 | if (Body.isInvalid) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 614 | Body = Actions.ActOnNullStmt(Tok.getLocation()); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 615 | // FIXME: Remove the case statement list from the Switch statement. |
| 616 | } |
| 617 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | ExitScope(); |
| 619 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 620 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /// ParseWhileStatement |
| 624 | /// while-statement: [C99 6.8.5.1] |
| 625 | /// 'while' '(' expression ')' statement |
| 626 | Parser::StmtResult Parser::ParseWhileStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 627 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 628 | SourceLocation WhileLoc = Tok.getLocation(); |
| 629 | ConsumeToken(); // eat the 'while'. |
| 630 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 631 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | Diag(Tok, diag::err_expected_lparen_after, "while"); |
| 633 | SkipUntil(tok::semi); |
| 634 | return true; |
| 635 | } |
| 636 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 637 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 638 | // the case for C90. Start the loop scope. |
| 639 | if (getLang().C99) |
| 640 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 641 | else |
| 642 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | |
| 644 | // Parse the condition. |
| 645 | ExprResult Cond = ParseSimpleParenExpression(); |
| 646 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 647 | // 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] | 648 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 649 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 650 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 651 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 652 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | // Read the body statement. |
| 654 | StmtResult Body = ParseStatement(); |
| 655 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 656 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 657 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 658 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 659 | ExitScope(); |
| 660 | |
| 661 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 662 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 663 | return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /// ParseDoStatement |
| 667 | /// do-statement: [C99 6.8.5.2] |
| 668 | /// 'do' statement 'while' '(' expression ')' ';' |
| 669 | /// Note: this lets the caller parse the end ';'. |
| 670 | Parser::StmtResult Parser::ParseDoStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 671 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 672 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
| 673 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 674 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 675 | // the case for C90. Start the loop scope. |
| 676 | if (getLang().C99) |
| 677 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 678 | else |
| 679 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 681 | // 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] | 682 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 683 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 684 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 685 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 686 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 687 | // Read the body statement. |
| 688 | StmtResult Body = ParseStatement(); |
| 689 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 690 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 691 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 693 | if (Tok.isNot(tok::kw_while)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 694 | ExitScope(); |
| 695 | Diag(Tok, diag::err_expected_while); |
| 696 | Diag(DoLoc, diag::err_matching, "do"); |
| 697 | SkipUntil(tok::semi); |
| 698 | return true; |
| 699 | } |
| 700 | SourceLocation WhileLoc = ConsumeToken(); |
| 701 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 702 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | ExitScope(); |
| 704 | Diag(Tok, diag::err_expected_lparen_after, "do/while"); |
| 705 | SkipUntil(tok::semi); |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | // Parse the condition. |
| 710 | ExprResult Cond = ParseSimpleParenExpression(); |
| 711 | |
| 712 | ExitScope(); |
| 713 | |
| 714 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 715 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 716 | return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /// ParseForStatement |
| 720 | /// for-statement: [C99 6.8.5.3] |
| 721 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 722 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
| 723 | Parser::StmtResult Parser::ParseForStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 724 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 725 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
| 726 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 727 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | Diag(Tok, diag::err_expected_lparen_after, "for"); |
| 729 | SkipUntil(tok::semi); |
| 730 | return true; |
| 731 | } |
| 732 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 733 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 734 | // the case for C90. Start the loop scope. |
| 735 | if (getLang().C99) |
| 736 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 737 | else |
| 738 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 739 | |
| 740 | SourceLocation LParenLoc = ConsumeParen(); |
| 741 | ExprResult Value; |
| 742 | |
| 743 | StmtTy *FirstPart = 0; |
| 744 | ExprTy *SecondPart = 0; |
| 745 | StmtTy *ThirdPart = 0; |
| 746 | |
| 747 | // Parse the first part of the for specifier. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 748 | if (Tok.is(tok::semi)) { // for (; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 749 | // no first part, eat the ';'. |
| 750 | ConsumeToken(); |
| 751 | } else if (isDeclarationSpecifier()) { // for (int X = 4; |
| 752 | // Parse declaration, which eats the ';'. |
| 753 | if (!getLang().C99) // Use of C99-style for loops in C90 mode? |
| 754 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
| 755 | DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 756 | StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 757 | FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val; |
| 758 | } else { |
| 759 | Value = ParseExpression(); |
| 760 | |
| 761 | // Turn the expression into a stmt. |
| 762 | if (!Value.isInvalid) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 763 | StmtResult R = Actions.ActOnExprStmt(Value.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | if (!R.isInvalid) |
| 765 | FirstPart = R.Val; |
| 766 | } |
| 767 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 768 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 769 | ConsumeToken(); |
| 770 | } else { |
| 771 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 772 | SkipUntil(tok::semi); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // Parse the second part of the for specifier. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 777 | if (Tok.is(tok::semi)) { // for (...;; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | // no second part. |
| 779 | Value = ExprResult(); |
| 780 | } else { |
| 781 | Value = ParseExpression(); |
| 782 | if (!Value.isInvalid) |
| 783 | SecondPart = Value.Val; |
| 784 | } |
| 785 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 786 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 787 | ConsumeToken(); |
| 788 | } else { |
| 789 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 790 | SkipUntil(tok::semi); |
| 791 | } |
| 792 | |
| 793 | // Parse the third part of the for specifier. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 794 | if (Tok.is(tok::r_paren)) { // for (...;...;) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | // no third part. |
| 796 | Value = ExprResult(); |
| 797 | } else { |
| 798 | Value = ParseExpression(); |
| 799 | if (!Value.isInvalid) { |
| 800 | // Turn the expression into a stmt. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 801 | StmtResult R = Actions.ActOnExprStmt(Value.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 802 | if (!R.isInvalid) |
| 803 | ThirdPart = R.Val; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // Match the ')'. |
| 808 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 809 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 810 | // 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] | 811 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 812 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 813 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 814 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 815 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | // Read the body statement. |
| 817 | StmtResult Body = ParseStatement(); |
| 818 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 819 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 820 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 822 | // Leave the for-scope. |
| 823 | ExitScope(); |
| 824 | |
| 825 | if (Body.isInvalid) |
| 826 | return Body; |
| 827 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 828 | return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart, SecondPart, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | ThirdPart, RParenLoc, Body.Val); |
| 830 | } |
| 831 | |
| 832 | /// ParseGotoStatement |
| 833 | /// jump-statement: |
| 834 | /// 'goto' identifier ';' |
| 835 | /// [GNU] 'goto' '*' expression ';' |
| 836 | /// |
| 837 | /// Note: this lets the caller parse the end ';'. |
| 838 | /// |
| 839 | Parser::StmtResult Parser::ParseGotoStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 840 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
| 842 | |
| 843 | StmtResult Res; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 844 | if (Tok.is(tok::identifier)) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 845 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 846 | Tok.getIdentifierInfo()); |
| 847 | ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 848 | } else if (Tok.is(tok::star) && !getLang().NoExtensions) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 849 | // GNU indirect goto extension. |
| 850 | Diag(Tok, diag::ext_gnu_indirect_goto); |
| 851 | SourceLocation StarLoc = ConsumeToken(); |
| 852 | ExprResult R = ParseExpression(); |
| 853 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 854 | SkipUntil(tok::semi, false, true); |
| 855 | return true; |
| 856 | } |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 857 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val); |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 858 | } else { |
| 859 | Diag(Tok, diag::err_expected_ident); |
| 860 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 862 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 863 | return Res; |
| 864 | } |
| 865 | |
| 866 | /// ParseContinueStatement |
| 867 | /// jump-statement: |
| 868 | /// 'continue' ';' |
| 869 | /// |
| 870 | /// Note: this lets the caller parse the end ';'. |
| 871 | /// |
| 872 | Parser::StmtResult Parser::ParseContinueStatement() { |
| 873 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 874 | return Actions.ActOnContinueStmt(ContinueLoc, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /// ParseBreakStatement |
| 878 | /// jump-statement: |
| 879 | /// 'break' ';' |
| 880 | /// |
| 881 | /// Note: this lets the caller parse the end ';'. |
| 882 | /// |
| 883 | Parser::StmtResult Parser::ParseBreakStatement() { |
| 884 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 885 | return Actions.ActOnBreakStmt(BreakLoc, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /// ParseReturnStatement |
| 889 | /// jump-statement: |
| 890 | /// 'return' expression[opt] ';' |
| 891 | Parser::StmtResult Parser::ParseReturnStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 892 | assert(Tok.is(tok::kw_return) && "Not a return stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 893 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
| 894 | |
| 895 | ExprResult R(0); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 896 | if (Tok.isNot(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | R = ParseExpression(); |
| 898 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 899 | SkipUntil(tok::semi, false, true); |
| 900 | return true; |
| 901 | } |
| 902 | } |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 903 | return Actions.ActOnReturnStmt(ReturnLoc, R.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
| 907 | /// [GNU] asm-statement: |
| 908 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 909 | /// |
| 910 | /// [GNU] asm-argument: |
| 911 | /// asm-string-literal |
| 912 | /// asm-string-literal ':' asm-operands[opt] |
| 913 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 914 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 915 | /// ':' asm-clobbers |
| 916 | /// |
| 917 | /// [GNU] asm-clobbers: |
| 918 | /// asm-string-literal |
| 919 | /// asm-clobbers ',' asm-string-literal |
| 920 | /// |
| 921 | Parser::StmtResult Parser::ParseAsmStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 922 | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 923 | SourceLocation AsmLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 924 | |
| 925 | DeclSpec DS; |
| 926 | SourceLocation Loc = Tok.getLocation(); |
| 927 | ParseTypeQualifierListOpt(DS); |
| 928 | |
| 929 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
| 930 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 931 | Diag(Loc, diag::w_asm_qualifier_ignored, "const"); |
| 932 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| 933 | Diag(Loc, diag::w_asm_qualifier_ignored, "restrict"); |
| 934 | |
| 935 | // Remember if this was a volatile asm. |
| 936 | //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile; |
| 937 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 938 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | Diag(Tok, diag::err_expected_lparen_after, "asm"); |
| 940 | SkipUntil(tok::r_paren); |
| 941 | return true; |
| 942 | } |
| 943 | Loc = ConsumeParen(); |
| 944 | |
| 945 | ParseAsmStringLiteral(); |
| 946 | |
| 947 | // Parse Outputs, if present. |
| 948 | ParseAsmOperandsOpt(); |
| 949 | |
| 950 | // Parse Inputs, if present. |
| 951 | ParseAsmOperandsOpt(); |
| 952 | |
| 953 | // Parse the clobbers, if present. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 954 | if (Tok.is(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 955 | ConsumeToken(); |
| 956 | |
| 957 | if (isTokenStringLiteral()) { |
| 958 | // Parse the asm-string list for clobbers. |
| 959 | while (1) { |
| 960 | ParseAsmStringLiteral(); |
| 961 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 962 | if (Tok.isNot(tok::comma)) break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 963 | ConsumeToken(); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 968 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 970 | // FIXME: Pass all the details down to the action. |
| 971 | return Actions.ActOnAsmStmt(AsmLoc, RParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /// ParseAsmOperands - Parse the asm-operands production as used by |
| 975 | /// asm-statement. We also parse a leading ':' token. If the leading colon is |
| 976 | /// not present, we do not parse anything. |
| 977 | /// |
| 978 | /// [GNU] asm-operands: |
| 979 | /// asm-operand |
| 980 | /// asm-operands ',' asm-operand |
| 981 | /// |
| 982 | /// [GNU] asm-operand: |
| 983 | /// asm-string-literal '(' expression ')' |
| 984 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 985 | /// |
| 986 | void Parser::ParseAsmOperandsOpt() { |
| 987 | // Only do anything if this operand is present. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 988 | if (Tok.isNot(tok::colon)) return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 989 | ConsumeToken(); |
| 990 | |
| 991 | // 'asm-operands' isn't present? |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 992 | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 993 | return; |
| 994 | |
| 995 | while (1) { |
| 996 | // Read the [id] if present. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 997 | if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 998 | SourceLocation Loc = ConsumeBracket(); |
| 999 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1000 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1001 | Diag(Tok, diag::err_expected_ident); |
| 1002 | SkipUntil(tok::r_paren); |
| 1003 | return; |
| 1004 | } |
Chris Lattner | 69efba7 | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Eat the identifier, FIXME: capture it. |
| 1007 | ConsumeToken(); |
| 1008 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1009 | MatchRHSPunctuation(tok::r_square, Loc); |
| 1010 | } |
| 1011 | |
| 1012 | ParseAsmStringLiteral(); |
| 1013 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1014 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | Diag(Tok, diag::err_expected_lparen_after, "asm operand"); |
| 1016 | SkipUntil(tok::r_paren); |
| 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | // Read the parenthesized expression. |
| 1021 | ExprResult Res = ParseSimpleParenExpression(); |
| 1022 | if (Res.isInvalid) { |
| 1023 | SkipUntil(tok::r_paren); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | // Eat the comma and continue parsing if it exists. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1028 | if (Tok.isNot(tok::comma)) return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1029 | ConsumeToken(); |
| 1030 | } |
| 1031 | } |