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