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