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