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