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