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