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