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