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 |
| 420 | /// |
| 421 | Parser::StmtResult Parser::ParseIfStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 422 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 423 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
| 424 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 425 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 426 | Diag(Tok, diag::err_expected_lparen_after, "if"); |
| 427 | SkipUntil(tok::semi); |
| 428 | return true; |
| 429 | } |
| 430 | |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 431 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 432 | // the case for C90. |
| 433 | if (getLang().C99) |
| 434 | EnterScope(Scope::DeclScope); |
| 435 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 436 | // Parse the condition. |
| 437 | ExprResult CondExp = ParseSimpleParenExpression(); |
| 438 | if (CondExp.isInvalid) { |
| 439 | SkipUntil(tok::semi); |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 440 | if (getLang().C99) |
| 441 | ExitScope(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 442 | return true; |
| 443 | } |
| 444 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 445 | // 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] | 446 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 447 | // 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] | 448 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 449 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | d190ac2 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 84b2071 | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 451 | // Read the 'then' stmt. |
| 452 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
| 453 | StmtResult ThenStmt = ParseStatement(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 454 | |
Chris Lattner | d190ac2 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 455 | // Pop the 'if' scope if needed. |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 456 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 457 | |
| 458 | // If it has an else, parse it. |
| 459 | SourceLocation ElseLoc; |
Chris Lattner | 84b2071 | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 460 | SourceLocation ElseStmtLoc; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 461 | StmtResult ElseStmt(false); |
Chris Lattner | 84b2071 | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 463 | if (Tok.is(tok::kw_else)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 464 | ElseLoc = ConsumeToken(); |
Chris Lattner | d190ac2 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 465 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 466 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 467 | // there is no compound stmt. C90 does not have this clause. We only do |
| 468 | // this if the body isn't a compound statement to avoid push/pop in common |
| 469 | // cases. |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 470 | NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 471 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | d190ac2 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 84b2071 | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 473 | ElseStmtLoc = Tok.getLocation(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 474 | ElseStmt = ParseStatement(); |
Chris Lattner | d190ac2 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 475 | |
| 476 | // Pop the 'else' scope if needed. |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 477 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 480 | if (getLang().C99) |
| 481 | ExitScope(); |
| 482 | |
Chris Lattner | 84b2071 | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 483 | // If the then or else stmt is invalid and the other is valid (and present), |
| 484 | // make turn the invalid one into a null stmt to avoid dropping the other |
| 485 | // part. If both are invalid, return error. |
| 486 | if ((ThenStmt.isInvalid && ElseStmt.isInvalid) || |
| 487 | (ThenStmt.isInvalid && ElseStmt.Val == 0) || |
| 488 | (ThenStmt.Val == 0 && ElseStmt.isInvalid)) { |
| 489 | // Both invalid, or one is invalid and other is non-present: delete cond and |
| 490 | // return error. |
| 491 | Actions.DeleteExpr(CondExp.Val); |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | // Now if either are invalid, replace with a ';'. |
| 496 | if (ThenStmt.isInvalid) |
| 497 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
| 498 | if (ElseStmt.isInvalid) |
| 499 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
| 500 | |
Chris Lattner | 84b2071 | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 501 | return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 502 | ElseLoc, ElseStmt.Val); |
| 503 | } |
| 504 | |
| 505 | /// ParseSwitchStatement |
| 506 | /// switch-statement: |
| 507 | /// 'switch' '(' expression ')' statement |
| 508 | Parser::StmtResult Parser::ParseSwitchStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 509 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 510 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
| 511 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 512 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 513 | Diag(Tok, diag::err_expected_lparen_after, "switch"); |
| 514 | SkipUntil(tok::semi); |
| 515 | return true; |
| 516 | } |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 517 | |
| 518 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 519 | // not the case for C90. Start the switch scope. |
| 520 | if (getLang().C99) |
| 521 | EnterScope(Scope::BreakScope|Scope::DeclScope); |
| 522 | else |
| 523 | EnterScope(Scope::BreakScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 524 | |
| 525 | // Parse the condition. |
| 526 | ExprResult Cond = ParseSimpleParenExpression(); |
| 527 | |
| 528 | if (Cond.isInvalid) { |
| 529 | ExitScope(); |
| 530 | return true; |
| 531 | } |
| 532 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 533 | StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 534 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 535 | // 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] | 536 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 537 | // 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] | 538 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 539 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 541 | // Read the body statement. |
| 542 | StmtResult Body = ParseStatement(); |
| 543 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 544 | // Pop the body scope if needed. |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 545 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 547 | if (Body.isInvalid) { |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 548 | Body = Actions.ActOnNullStmt(Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 549 | // FIXME: Remove the case statement list from the Switch statement. |
| 550 | } |
| 551 | |
| 552 | ExitScope(); |
| 553 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 554 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /// ParseWhileStatement |
| 558 | /// while-statement: [C99 6.8.5.1] |
| 559 | /// 'while' '(' expression ')' statement |
| 560 | Parser::StmtResult Parser::ParseWhileStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 561 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 562 | SourceLocation WhileLoc = Tok.getLocation(); |
| 563 | ConsumeToken(); // eat the 'while'. |
| 564 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 565 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 566 | Diag(Tok, diag::err_expected_lparen_after, "while"); |
| 567 | SkipUntil(tok::semi); |
| 568 | return true; |
| 569 | } |
| 570 | |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 571 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 572 | // the case for C90. Start the loop scope. |
| 573 | if (getLang().C99) |
| 574 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 575 | else |
| 576 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 577 | |
| 578 | // Parse the condition. |
| 579 | ExprResult Cond = ParseSimpleParenExpression(); |
| 580 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 581 | // 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] | 582 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 583 | // 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] | 584 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 585 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 587 | // Read the body statement. |
| 588 | StmtResult Body = ParseStatement(); |
| 589 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 590 | // Pop the body scope if needed. |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 591 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 593 | ExitScope(); |
| 594 | |
| 595 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 596 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 597 | return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /// ParseDoStatement |
| 601 | /// do-statement: [C99 6.8.5.2] |
| 602 | /// 'do' statement 'while' '(' expression ')' ';' |
| 603 | /// Note: this lets the caller parse the end ';'. |
| 604 | Parser::StmtResult Parser::ParseDoStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 605 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 606 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
| 607 | |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 608 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 609 | // the case for C90. Start the loop scope. |
| 610 | if (getLang().C99) |
| 611 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 612 | else |
| 613 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 614 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 615 | // 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] | 616 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 617 | // 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] | 618 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 619 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 620 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 621 | // Read the body statement. |
| 622 | StmtResult Body = ParseStatement(); |
| 623 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 624 | // Pop the body scope if needed. |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 625 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 627 | if (Tok.isNot(tok::kw_while)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 628 | ExitScope(); |
| 629 | Diag(Tok, diag::err_expected_while); |
| 630 | Diag(DoLoc, diag::err_matching, "do"); |
| 631 | SkipUntil(tok::semi); |
| 632 | return true; |
| 633 | } |
| 634 | SourceLocation WhileLoc = ConsumeToken(); |
| 635 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 636 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 637 | ExitScope(); |
| 638 | Diag(Tok, diag::err_expected_lparen_after, "do/while"); |
| 639 | SkipUntil(tok::semi); |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | // Parse the condition. |
| 644 | ExprResult Cond = ParseSimpleParenExpression(); |
| 645 | |
| 646 | ExitScope(); |
| 647 | |
| 648 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 649 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 650 | return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /// ParseForStatement |
| 654 | /// for-statement: [C99 6.8.5.3] |
| 655 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 656 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 657 | /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement |
| 658 | /// [OBJC2] 'for' '(' expr 'in' expr ')' statement |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 659 | Parser::StmtResult Parser::ParseForStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 660 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 661 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
| 662 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 663 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 664 | Diag(Tok, diag::err_expected_lparen_after, "for"); |
| 665 | SkipUntil(tok::semi); |
| 666 | return true; |
| 667 | } |
| 668 | |
Chris Lattner | e0cc508 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 669 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 670 | // the case for C90. Start the loop scope. |
| 671 | if (getLang().C99) |
| 672 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 673 | else |
| 674 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 675 | |
| 676 | SourceLocation LParenLoc = ConsumeParen(); |
| 677 | ExprResult Value; |
| 678 | |
| 679 | StmtTy *FirstPart = 0; |
| 680 | ExprTy *SecondPart = 0; |
| 681 | StmtTy *ThirdPart = 0; |
Fariborz Jahanian | 6e9c2b1 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 682 | bool ForEach = false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 683 | |
| 684 | // Parse the first part of the for specifier. |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 685 | if (Tok.is(tok::semi)) { // for (; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 686 | // no first part, eat the ';'. |
| 687 | ConsumeToken(); |
| 688 | } else if (isDeclarationSpecifier()) { // for (int X = 4; |
| 689 | // Parse declaration, which eats the ';'. |
| 690 | if (!getLang().C99) // Use of C99-style for loops in C90 mode? |
| 691 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
Chris Lattner | a4ff427 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 692 | |
| 693 | SourceLocation DeclStart = Tok.getLocation(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 694 | DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext); |
Chris Lattner | a4ff427 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 695 | // FIXME: Pass in the right location for the end of the declstmt. |
| 696 | StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart, |
Chris Lattner | daf1c31 | 2008-03-13 06:29:54 +0000 | [diff] [blame] | 697 | DeclStart); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 698 | FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val; |
Fariborz Jahanian | 6e9c2b1 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 699 | if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 700 | ConsumeToken(); // consume 'in' |
| 701 | Value = ParseExpression(); |
| 702 | if (!Value.isInvalid) |
| 703 | SecondPart = Value.Val; |
| 704 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 705 | } else { |
| 706 | Value = ParseExpression(); |
| 707 | |
| 708 | // Turn the expression into a stmt. |
| 709 | if (!Value.isInvalid) { |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 710 | StmtResult R = Actions.ActOnExprStmt(Value.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 711 | if (!R.isInvalid) |
| 712 | FirstPart = R.Val; |
| 713 | } |
| 714 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 715 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 716 | ConsumeToken(); |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 717 | } |
Fariborz Jahanian | 6e9c2b1 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 718 | else if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 719 | ConsumeToken(); // consume 'in' |
| 720 | Value = ParseExpression(); |
| 721 | if (!Value.isInvalid) |
| 722 | SecondPart = Value.Val; |
| 723 | } |
| 724 | else { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 725 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 726 | SkipUntil(tok::semi); |
| 727 | } |
| 728 | } |
Fariborz Jahanian | 6e9c2b1 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 729 | if (!ForEach) { |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 730 | // Parse the second part of the for specifier. |
| 731 | if (Tok.is(tok::semi)) { // for (...;; |
| 732 | // no second part. |
| 733 | Value = ExprResult(); |
| 734 | } else { |
| 735 | Value = ParseExpression(); |
| 736 | if (!Value.isInvalid) |
| 737 | SecondPart = Value.Val; |
| 738 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 739 | |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 740 | if (Tok.is(tok::semi)) { |
| 741 | ConsumeToken(); |
| 742 | } else { |
| 743 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 744 | SkipUntil(tok::semi); |
| 745 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 746 | |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 747 | // Parse the third part of the for specifier. |
| 748 | if (Tok.is(tok::r_paren)) { // for (...;...;) |
| 749 | // no third part. |
| 750 | Value = ExprResult(); |
| 751 | } else { |
| 752 | Value = ParseExpression(); |
| 753 | if (!Value.isInvalid) { |
| 754 | // Turn the expression into a stmt. |
| 755 | StmtResult R = Actions.ActOnExprStmt(Value.Val); |
| 756 | if (!R.isInvalid) |
| 757 | ThirdPart = R.Val; |
| 758 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 761 | // Match the ')'. |
| 762 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 763 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 764 | // 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] | 765 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 766 | // 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] | 767 | bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 768 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 770 | // Read the body statement. |
| 771 | StmtResult Body = ParseStatement(); |
| 772 | |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 773 | // Pop the body scope if needed. |
Chris Lattner | 59ed6e2 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 774 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | f446f72 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 776 | // Leave the for-scope. |
| 777 | ExitScope(); |
| 778 | |
| 779 | if (Body.isInvalid) |
| 780 | return Body; |
| 781 | |
Fariborz Jahanian | 6e9c2b1 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 782 | if (!ForEach) |
| 783 | return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart, |
| 784 | SecondPart, ThirdPart, RParenLoc, Body.Val); |
| 785 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 786 | return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart, |
Fariborz Jahanian | 6e9c2b1 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 787 | SecondPart, RParenLoc, Body.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /// ParseGotoStatement |
| 791 | /// jump-statement: |
| 792 | /// 'goto' identifier ';' |
| 793 | /// [GNU] 'goto' '*' expression ';' |
| 794 | /// |
| 795 | /// Note: this lets the caller parse the end ';'. |
| 796 | /// |
| 797 | Parser::StmtResult Parser::ParseGotoStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 798 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 799 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
| 800 | |
| 801 | StmtResult Res; |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 802 | if (Tok.is(tok::identifier)) { |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 803 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 804 | Tok.getIdentifierInfo()); |
| 805 | ConsumeToken(); |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 806 | } else if (Tok.is(tok::star) && !getLang().NoExtensions) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 807 | // GNU indirect goto extension. |
| 808 | Diag(Tok, diag::ext_gnu_indirect_goto); |
| 809 | SourceLocation StarLoc = ConsumeToken(); |
| 810 | ExprResult R = ParseExpression(); |
| 811 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 812 | SkipUntil(tok::semi, false, true); |
| 813 | return true; |
| 814 | } |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 815 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 816 | } else { |
| 817 | Diag(Tok, diag::err_expected_ident); |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | return Res; |
| 822 | } |
| 823 | |
| 824 | /// ParseContinueStatement |
| 825 | /// jump-statement: |
| 826 | /// 'continue' ';' |
| 827 | /// |
| 828 | /// Note: this lets the caller parse the end ';'. |
| 829 | /// |
| 830 | Parser::StmtResult Parser::ParseContinueStatement() { |
| 831 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 832 | return Actions.ActOnContinueStmt(ContinueLoc, CurScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /// ParseBreakStatement |
| 836 | /// jump-statement: |
| 837 | /// 'break' ';' |
| 838 | /// |
| 839 | /// Note: this lets the caller parse the end ';'. |
| 840 | /// |
| 841 | Parser::StmtResult Parser::ParseBreakStatement() { |
| 842 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 843 | return Actions.ActOnBreakStmt(BreakLoc, CurScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /// ParseReturnStatement |
| 847 | /// jump-statement: |
| 848 | /// 'return' expression[opt] ';' |
| 849 | Parser::StmtResult Parser::ParseReturnStatement() { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 850 | assert(Tok.is(tok::kw_return) && "Not a return stmt!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 851 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
| 852 | |
| 853 | ExprResult R(0); |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 854 | if (Tok.isNot(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 855 | R = ParseExpression(); |
| 856 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 857 | SkipUntil(tok::semi, false, true); |
| 858 | return true; |
| 859 | } |
| 860 | } |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 861 | return Actions.ActOnReturnStmt(ReturnLoc, R.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Steve Naroff | 6f9f955 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 864 | /// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this |
| 865 | /// 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] | 866 | Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() { |
Steve Naroff | be880ec | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 867 | if (Tok.is(tok::l_brace)) { |
| 868 | unsigned short savedBraceCount = BraceCount; |
| 869 | do { |
| 870 | ConsumeAnyToken(); |
| 871 | } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof)); |
| 872 | } else { |
| 873 | // From the MS website: If used without braces, the __asm keyword means |
| 874 | // that the rest of the line is an assembly-language statement. |
| 875 | SourceManager &SrcMgr = PP.getSourceManager(); |
Steve Naroff | ab3dfe0 | 2008-02-08 03:36:19 +0000 | [diff] [blame] | 876 | SourceLocation TokLoc = Tok.getLocation(); |
Steve Naroff | 8ce442a | 2008-02-08 18:01:27 +0000 | [diff] [blame] | 877 | unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc); |
| 878 | do { |
| 879 | ConsumeAnyToken(); |
| 880 | TokLoc = Tok.getLocation(); |
| 881 | } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) && |
| 882 | Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) && |
| 883 | Tok.isNot(tok::eof)); |
Steve Naroff | be880ec | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 884 | } |
Steve Naroff | 6a52281 | 2008-04-07 21:06:54 +0000 | [diff] [blame] | 885 | return Actions.ActOnNullStmt(Tok.getLocation()); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 888 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
Steve Naroff | 6f9f955 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 889 | /// asm-statement: |
| 890 | /// gnu-asm-statement |
| 891 | /// ms-asm-statement |
| 892 | /// |
| 893 | /// [GNU] gnu-asm-statement: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 894 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 895 | /// |
| 896 | /// [GNU] asm-argument: |
| 897 | /// asm-string-literal |
| 898 | /// asm-string-literal ':' asm-operands[opt] |
| 899 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 900 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 901 | /// ':' asm-clobbers |
| 902 | /// |
| 903 | /// [GNU] asm-clobbers: |
| 904 | /// asm-string-literal |
| 905 | /// asm-clobbers ',' asm-string-literal |
| 906 | /// |
Steve Naroff | 6f9f955 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 907 | /// [MS] ms-asm-statement: |
| 908 | /// '__asm' assembly-instruction ';'[opt] |
| 909 | /// '__asm' '{' assembly-instruction-list '}' ';'[opt] |
| 910 | /// |
| 911 | /// [MS] assembly-instruction-list: |
| 912 | /// assembly-instruction ';'[opt] |
| 913 | /// assembly-instruction-list ';' assembly-instruction ';'[opt] |
| 914 | /// |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 915 | Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) { |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 916 | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
Chris Lattner | 8a40a83 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 917 | SourceLocation AsmLoc = ConsumeToken(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 918 | |
Steve Naroff | 6f9f955 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 919 | if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) { |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 920 | msAsm = true; |
| 921 | return FuzzyParseMicrosoftAsmStatement(); |
| 922 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 923 | DeclSpec DS; |
| 924 | SourceLocation Loc = Tok.getLocation(); |
| 925 | ParseTypeQualifierListOpt(DS); |
| 926 | |
| 927 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
| 928 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 929 | Diag(Loc, diag::w_asm_qualifier_ignored, "const"); |
| 930 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| 931 | Diag(Loc, diag::w_asm_qualifier_ignored, "restrict"); |
| 932 | |
| 933 | // Remember if this was a volatile asm. |
Anders Carlsson | 759f45d | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 934 | bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile; |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 935 | bool isSimple = false; |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 936 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 937 | Diag(Tok, diag::err_expected_lparen_after, "asm"); |
| 938 | SkipUntil(tok::r_paren); |
| 939 | return true; |
| 940 | } |
| 941 | Loc = ConsumeParen(); |
| 942 | |
Anders Carlsson | 076c111 | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 943 | ExprResult AsmString = ParseAsmStringLiteral(); |
| 944 | if (AsmString.isInvalid) |
| 945 | return true; |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 946 | |
| 947 | llvm::SmallVector<std::string, 4> Names; |
| 948 | llvm::SmallVector<ExprTy*, 4> Constraints; |
| 949 | llvm::SmallVector<ExprTy*, 4> Exprs; |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 950 | llvm::SmallVector<ExprTy*, 4> Clobbers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 951 | |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 952 | unsigned NumInputs = 0, NumOutputs = 0; |
| 953 | |
| 954 | SourceLocation RParenLoc; |
| 955 | if (Tok.is(tok::r_paren)) { |
| 956 | // We have a simple asm expression |
| 957 | isSimple = true; |
| 958 | |
| 959 | RParenLoc = ConsumeParen(); |
| 960 | } else { |
| 961 | // Parse Outputs, if present. |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 962 | if (ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
| 963 | return true; |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 964 | |
| 965 | NumOutputs = Names.size(); |
| 966 | |
| 967 | // Parse Inputs, if present. |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 968 | if (ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
| 969 | return true; |
| 970 | |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 971 | assert(Names.size() == Constraints.size() && |
| 972 | Constraints.size() == Exprs.size() |
| 973 | && "Input operand size mismatch!"); |
| 974 | |
| 975 | NumInputs = Names.size() - NumOutputs; |
| 976 | |
| 977 | // Parse the clobbers, if present. |
| 978 | if (Tok.is(tok::colon)) { |
Anders Carlsson | 861a285 | 2007-11-21 23:27:34 +0000 | [diff] [blame] | 979 | ConsumeToken(); |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 980 | |
| 981 | // Parse the asm-string list for clobbers. |
| 982 | while (1) { |
| 983 | ExprResult Clobber = ParseAsmStringLiteral(); |
| 984 | |
| 985 | if (Clobber.isInvalid) |
| 986 | break; |
| 987 | |
| 988 | Clobbers.push_back(Clobber.Val); |
| 989 | |
| 990 | if (Tok.isNot(tok::comma)) break; |
| 991 | ConsumeToken(); |
| 992 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 993 | } |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 994 | |
| 995 | RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Anders Carlsson | de6a9c4 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 998 | return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile, |
| 999 | NumOutputs, NumInputs, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1000 | &Names[0], &Constraints[0], &Exprs[0], |
| 1001 | AsmString.Val, |
| 1002 | Clobbers.size(), &Clobbers[0], |
| 1003 | RParenLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /// ParseAsmOperands - Parse the asm-operands production as used by |
| 1007 | /// asm-statement. We also parse a leading ':' token. If the leading colon is |
| 1008 | /// not present, we do not parse anything. |
| 1009 | /// |
| 1010 | /// [GNU] asm-operands: |
| 1011 | /// asm-operand |
| 1012 | /// asm-operands ',' asm-operand |
| 1013 | /// |
| 1014 | /// [GNU] asm-operand: |
| 1015 | /// asm-string-literal '(' expression ')' |
| 1016 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 1017 | /// |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1018 | bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1019 | llvm::SmallVectorImpl<ExprTy*> &Constraints, |
| 1020 | llvm::SmallVectorImpl<ExprTy*> &Exprs) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1021 | // Only do anything if this operand is present. |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1022 | if (Tok.isNot(tok::colon)) return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1023 | ConsumeToken(); |
| 1024 | |
| 1025 | // 'asm-operands' isn't present? |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1026 | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1027 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1028 | |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1029 | while (1) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1030 | // Read the [id] if present. |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1031 | if (Tok.is(tok::l_square)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1032 | SourceLocation Loc = ConsumeBracket(); |
| 1033 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1034 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1035 | Diag(Tok, diag::err_expected_ident); |
| 1036 | SkipUntil(tok::r_paren); |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1037 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1038 | } |
Chris Lattner | 4e21a9b | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 1039 | |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1040 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 4e21a9b | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 1041 | ConsumeToken(); |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1042 | |
| 1043 | Names.push_back(std::string(II->getName(), II->getLength())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1044 | MatchRHSPunctuation(tok::r_square, Loc); |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1045 | } else |
| 1046 | Names.push_back(std::string()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1047 | |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1048 | ExprResult Constraint = ParseAsmStringLiteral(); |
| 1049 | if (Constraint.isInvalid) { |
| 1050 | SkipUntil(tok::r_paren); |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1051 | return true; |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1052 | } |
| 1053 | Constraints.push_back(Constraint.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1055 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1056 | Diag(Tok, diag::err_expected_lparen_after, "asm operand"); |
| 1057 | SkipUntil(tok::r_paren); |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1058 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | // Read the parenthesized expression. |
| 1062 | ExprResult Res = ParseSimpleParenExpression(); |
| 1063 | if (Res.isInvalid) { |
| 1064 | SkipUntil(tok::r_paren); |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1065 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1066 | } |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1067 | Exprs.push_back(Res.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1068 | // Eat the comma and continue parsing if it exists. |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1069 | if (Tok.isNot(tok::comma)) return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1070 | ConsumeToken(); |
| 1071 | } |
Anders Carlsson | 749d7b0 | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1072 | |
| 1073 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1074 | } |
Fariborz Jahanian | 829dfe5 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1075 | |
| 1076 | Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl, |
| 1077 | SourceLocation L, SourceLocation R) { |
| 1078 | // Do not enter a scope for the brace, as the arguments are in the same scope |
| 1079 | // (the function body) as the body itself. Instead, just read the statement |
| 1080 | // list and put it into a CompoundStmt for safe keeping. |
| 1081 | StmtResult FnBody = ParseCompoundStatementBody(); |
| 1082 | |
| 1083 | // If the function body could not be parsed, make a bogus compoundstmt. |
| 1084 | if (FnBody.isInvalid) |
| 1085 | FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false); |
| 1086 | |
| 1087 | // Leave the function body scope. |
| 1088 | ExitScope(); |
| 1089 | |
Steve Naroff | 99ee430 | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1090 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val); |
Seo Sanghyeon | e128c1d | 2007-12-01 08:06:07 +0000 | [diff] [blame] | 1091 | } |