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