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