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