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 | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 16 | #include "RAIIObjectsForParser.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 18 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Scope.h" |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/TypoCorrection.h" |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
| 22 | #include "clang/Basic/PrettyStackTrace.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
Chad Rosier | 8cd64b4 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // C99 6.8: Statements and Blocks. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 32 | /// StatementOrDeclaration: |
| 33 | /// statement |
| 34 | /// declaration |
| 35 | /// |
| 36 | /// statement: |
| 37 | /// labeled-statement |
| 38 | /// compound-statement |
| 39 | /// expression-statement |
| 40 | /// selection-statement |
| 41 | /// iteration-statement |
| 42 | /// jump-statement |
Argyrios Kyrtzidis | dcdd55f | 2008-09-07 18:58:01 +0000 | [diff] [blame] | 43 | /// [C++] declaration-statement |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 44 | /// [C++] try-block |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 45 | /// [MS] seh-try-block |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 46 | /// [OBC] objc-throw-statement |
| 47 | /// [OBC] objc-try-catch-statement |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 48 | /// [OBC] objc-synchronized-statement |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | /// [GNU] asm-statement |
| 50 | /// [OMP] openmp-construct [TODO] |
| 51 | /// |
| 52 | /// labeled-statement: |
| 53 | /// identifier ':' statement |
| 54 | /// 'case' constant-expression ':' statement |
| 55 | /// 'default' ':' statement |
| 56 | /// |
| 57 | /// selection-statement: |
| 58 | /// if-statement |
| 59 | /// switch-statement |
| 60 | /// |
| 61 | /// iteration-statement: |
| 62 | /// while-statement |
| 63 | /// do-statement |
| 64 | /// for-statement |
| 65 | /// |
| 66 | /// expression-statement: |
| 67 | /// expression[opt] ';' |
| 68 | /// |
| 69 | /// jump-statement: |
| 70 | /// 'goto' identifier ';' |
| 71 | /// 'continue' ';' |
| 72 | /// 'break' ';' |
| 73 | /// 'return' expression[opt] ';' |
| 74 | /// [GNU] 'goto' '*' expression ';' |
| 75 | /// |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 76 | /// [OBC] objc-throw-statement: |
| 77 | /// [OBC] '@' 'throw' expression ';' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | /// [OBC] '@' 'throw' ';' |
| 79 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 80 | StmtResult |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 81 | Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement, |
| 82 | SourceLocation *TrailingElseLoc) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 83 | |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 84 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 85 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 86 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 87 | MaybeParseCXX0XAttributes(Attrs, 0, /*MightBeObjCMessageSend*/ true); |
| 88 | |
| 89 | StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts, |
| 90 | OnlyStatement, TrailingElseLoc, Attrs); |
| 91 | |
| 92 | assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) && |
| 93 | "attributes on empty statement"); |
| 94 | |
| 95 | if (Attrs.empty() || Res.isInvalid()) |
| 96 | return Res; |
| 97 | |
| 98 | return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range); |
| 99 | } |
| 100 | |
| 101 | StmtResult |
| 102 | Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts, |
| 103 | bool OnlyStatement, SourceLocation *TrailingElseLoc, |
| 104 | ParsedAttributesWithRange &Attrs) { |
| 105 | const char *SemiError = 0; |
| 106 | StmtResult Res; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // Cases in this switch statement should fall through if the parser expects |
| 109 | // the token to end in a semicolon (in which case SemiError should be set), |
| 110 | // or they directly 'return;' if not. |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 111 | Retry: |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 112 | tok::TokenKind Kind = Tok.getKind(); |
| 113 | SourceLocation AtLoc; |
| 114 | switch (Kind) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 115 | case tok::at: // May be a @try or @throw statement |
| 116 | { |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 117 | ProhibitAttributes(Attrs); // TODO: is it correct? |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 118 | AtLoc = ConsumeToken(); // consume @ |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 119 | return ParseObjCAtStatement(AtLoc); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 120 | } |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 122 | case tok::code_completion: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 123 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 124 | cutOffParsing(); |
| 125 | return StmtError(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 127 | case tok::identifier: { |
| 128 | Token Next = NextToken(); |
| 129 | if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement |
Argyrios Kyrtzidis | b9f930d | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 130 | // identifier ':' statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 131 | return ParseLabeledStatement(Attrs); |
Argyrios Kyrtzidis | b9f930d | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 132 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 133 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 134 | // Look up the identifier, and typo-correct it to a keyword if it's not |
| 135 | // found. |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 136 | if (Next.isNot(tok::coloncolon)) { |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 137 | // Try to limit which sets of keywords should be included in typo |
| 138 | // correction based on what the next token is. |
| 139 | // FIXME: Pass the next token into the CorrectionCandidateCallback and |
| 140 | // do this filtering in a more fine-grained manner. |
| 141 | CorrectionCandidateCallback DefaultValidator; |
| 142 | DefaultValidator.WantTypeSpecifiers = |
| 143 | Next.is(tok::l_paren) || Next.is(tok::less) || |
| 144 | Next.is(tok::identifier) || Next.is(tok::star) || |
| 145 | Next.is(tok::amp) || Next.is(tok::l_square); |
| 146 | DefaultValidator.WantExpressionKeywords = |
| 147 | Next.is(tok::l_paren) || Next.is(tok::identifier) || |
| 148 | Next.is(tok::arrow) || Next.is(tok::period); |
| 149 | DefaultValidator.WantRemainingKeywords = |
| 150 | Next.is(tok::l_paren) || Next.is(tok::semi) || |
| 151 | Next.is(tok::identifier) || Next.is(tok::l_brace); |
| 152 | DefaultValidator.WantCXXNamedCasts = false; |
| 153 | if (TryAnnotateName(/*IsAddressOfOperand*/false, &DefaultValidator) |
| 154 | == ANK_Error) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 155 | // Handle errors here by skipping up to the next semicolon or '}', and |
| 156 | // eat the semicolon if that's what stopped us. |
| 157 | SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 158 | if (Tok.is(tok::semi)) |
| 159 | ConsumeToken(); |
| 160 | return StmtError(); |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 161 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 162 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 163 | // If the identifier was typo-corrected, try again. |
| 164 | if (Tok.isNot(tok::identifier)) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 165 | goto Retry; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 166 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 168 | // Fall through |
| 169 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 170 | |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 171 | default: { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 172 | if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) { |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 173 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 174 | DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext, |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 175 | DeclEnd, Attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 176 | return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd); |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | Diag(Tok, diag::err_expected_statement); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 181 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 184 | return ParseExprStatement(); |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 185 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | case tok::kw_case: // C99 6.8.1: labeled-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 188 | return ParseCaseStatement(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | case tok::kw_default: // C99 6.8.1: labeled-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 190 | return ParseDefaultStatement(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | case tok::l_brace: // C99 6.8.2: compound-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 193 | return ParseCompoundStatement(); |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 194 | case tok::semi: { // C99 6.8.3p3: expression[opt] ';' |
Argyrios Kyrtzidis | e2ca828 | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 195 | bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro(); |
| 196 | return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro); |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 197 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | case tok::kw_if: // C99 6.8.4.1: if-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 200 | return ParseIfStatement(TrailingElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 202 | return ParseSwitchStatement(TrailingElseLoc); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | case tok::kw_while: // C99 6.8.5.1: while-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 205 | return ParseWhileStatement(TrailingElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | case tok::kw_do: // C99 6.8.5.2: do-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 207 | Res = ParseDoStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 208 | SemiError = "do/while"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | break; |
| 210 | case tok::kw_for: // C99 6.8.5.3: for-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 211 | return ParseForStatement(TrailingElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | |
| 213 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 214 | Res = ParseGotoStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 215 | SemiError = "goto"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | break; |
| 217 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 218 | Res = ParseContinueStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 219 | SemiError = "continue"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | break; |
| 221 | case tok::kw_break: // C99 6.8.6.3: break-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 222 | Res = ParseBreakStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 223 | SemiError = "break"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | break; |
| 225 | case tok::kw_return: // C99 6.8.6.4: return-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 226 | Res = ParseReturnStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 227 | SemiError = "return"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | break; |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 229 | |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 230 | case tok::kw_asm: { |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 231 | ProhibitAttributes(Attrs); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 232 | bool msAsm = false; |
| 233 | Res = ParseAsmStatement(msAsm); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 234 | Res = Actions.ActOnFinishFullStmt(Res.get()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 235 | if (msAsm) return Res; |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 236 | SemiError = "asm"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 239 | |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 240 | case tok::kw_try: // C++ 15: try-block |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 241 | return ParseCXXTryBlock(); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 242 | |
| 243 | case tok::kw___try: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 244 | ProhibitAttributes(Attrs); // TODO: is it correct? |
| 245 | return ParseSEHTryBlock(); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 246 | |
| 247 | case tok::annot_pragma_vis: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 248 | ProhibitAttributes(Attrs); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 249 | HandlePragmaVisibility(); |
| 250 | return StmtEmpty(); |
| 251 | |
| 252 | case tok::annot_pragma_pack: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 253 | ProhibitAttributes(Attrs); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 254 | HandlePragmaPack(); |
| 255 | return StmtEmpty(); |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame^] | 256 | |
| 257 | case tok::annot_pragma_fp_contract: |
| 258 | ProhibitAttributes(Attrs); |
| 259 | HandlePragmaFPContract(); |
| 260 | return StmtEmpty(); |
| 261 | |
| 262 | case tok::annot_pragma_opencl_extension: |
| 263 | ProhibitAttributes(Attrs); |
| 264 | HandlePragmaOpenCLExtension(); |
| 265 | return StmtEmpty(); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | // If we reached this code, the statement must end in a semicolon. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 269 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 271 | } else if (!Res.isInvalid()) { |
Chris Lattner | 7b3684a | 2009-06-14 00:23:56 +0000 | [diff] [blame] | 272 | // If the result was valid, then we do want to diagnose this. Use |
| 273 | // ExpectAndConsume to emit the diagnostic, even though we know it won't |
| 274 | // succeed. |
| 275 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError); |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 276 | // Skip until we see a } or ;, but don't eat it. |
| 277 | SkipUntil(tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 280 | return Res; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 283 | /// \brief Parse an expression statement. |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 284 | StmtResult Parser::ParseExprStatement() { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 285 | // If a case keyword is missing, this is where it should be inserted. |
| 286 | Token OldToken = Tok; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 288 | // expression[opt] ';' |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 289 | ExprResult Expr(ParseExpression()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 290 | if (Expr.isInvalid()) { |
| 291 | // If the expression is invalid, skip ahead to the next semicolon or '}'. |
| 292 | // Not doing this opens us up to the possibility of infinite loops if |
| 293 | // ParseExpression does not consume any tokens. |
| 294 | SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 295 | if (Tok.is(tok::semi)) |
| 296 | ConsumeToken(); |
| 297 | return StmtError(); |
| 298 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 300 | if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() && |
| 301 | Actions.CheckCaseExpression(Expr.get())) { |
| 302 | // If a constant expression is followed by a colon inside a switch block, |
| 303 | // suggest a missing case keyword. |
| 304 | Diag(OldToken, diag::err_expected_case_before_expression) |
| 305 | << FixItHint::CreateInsertion(OldToken.getLocation(), "case "); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 307 | // Recover parsing as a case statement. |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 308 | return ParseCaseStatement(/*MissingCase=*/true, Expr); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 309 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 310 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 311 | // Otherwise, eat the semicolon. |
| 312 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
| 313 | return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get())); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 314 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 315 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 316 | StmtResult Parser::ParseSEHTryBlock() { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 317 | assert(Tok.is(tok::kw___try) && "Expected '__try'"); |
| 318 | SourceLocation Loc = ConsumeToken(); |
| 319 | return ParseSEHTryBlockCommon(Loc); |
| 320 | } |
| 321 | |
| 322 | /// ParseSEHTryBlockCommon |
| 323 | /// |
| 324 | /// seh-try-block: |
| 325 | /// '__try' compound-statement seh-handler |
| 326 | /// |
| 327 | /// seh-handler: |
| 328 | /// seh-except-block |
| 329 | /// seh-finally-block |
| 330 | /// |
| 331 | StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) { |
| 332 | if(Tok.isNot(tok::l_brace)) |
| 333 | return StmtError(Diag(Tok,diag::err_expected_lbrace)); |
| 334 | |
Joao Matos | 568ba87 | 2012-09-04 17:49:35 +0000 | [diff] [blame] | 335 | StmtResult TryBlock(ParseCompoundStatement()); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 336 | if(TryBlock.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 337 | return TryBlock; |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 338 | |
| 339 | StmtResult Handler; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 340 | if (Tok.is(tok::identifier) && |
Douglas Gregor | b57791e | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 341 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 342 | SourceLocation Loc = ConsumeToken(); |
| 343 | Handler = ParseSEHExceptBlock(Loc); |
| 344 | } else if (Tok.is(tok::kw___finally)) { |
| 345 | SourceLocation Loc = ConsumeToken(); |
| 346 | Handler = ParseSEHFinallyBlock(Loc); |
| 347 | } else { |
| 348 | return StmtError(Diag(Tok,diag::err_seh_expected_handler)); |
| 349 | } |
| 350 | |
| 351 | if(Handler.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 352 | return Handler; |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 353 | |
| 354 | return Actions.ActOnSEHTryBlock(false /* IsCXXTry */, |
| 355 | TryLoc, |
| 356 | TryBlock.take(), |
| 357 | Handler.take()); |
| 358 | } |
| 359 | |
| 360 | /// ParseSEHExceptBlock - Handle __except |
| 361 | /// |
| 362 | /// seh-except-block: |
| 363 | /// '__except' '(' seh-filter-expression ')' compound-statement |
| 364 | /// |
| 365 | StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) { |
| 366 | PoisonIdentifierRAIIObject raii(Ident__exception_code, false), |
| 367 | raii2(Ident___exception_code, false), |
| 368 | raii3(Ident_GetExceptionCode, false); |
| 369 | |
| 370 | if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen)) |
| 371 | return StmtError(); |
| 372 | |
| 373 | ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope); |
| 374 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 375 | if (getLangOpts().Borland) { |
Francois Pichet | d7f02df | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 376 | Ident__exception_info->setIsPoisoned(false); |
| 377 | Ident___exception_info->setIsPoisoned(false); |
| 378 | Ident_GetExceptionInfo->setIsPoisoned(false); |
| 379 | } |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 380 | ExprResult FilterExpr(ParseExpression()); |
Francois Pichet | d7f02df | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 381 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 382 | if (getLangOpts().Borland) { |
Francois Pichet | d7f02df | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 383 | Ident__exception_info->setIsPoisoned(true); |
| 384 | Ident___exception_info->setIsPoisoned(true); |
| 385 | Ident_GetExceptionInfo->setIsPoisoned(true); |
| 386 | } |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 387 | |
| 388 | if(FilterExpr.isInvalid()) |
| 389 | return StmtError(); |
| 390 | |
| 391 | if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen)) |
| 392 | return StmtError(); |
| 393 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 394 | StmtResult Block(ParseCompoundStatement()); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 395 | |
| 396 | if(Block.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 397 | return Block; |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 398 | |
| 399 | return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take()); |
| 400 | } |
| 401 | |
| 402 | /// ParseSEHFinallyBlock - Handle __finally |
| 403 | /// |
| 404 | /// seh-finally-block: |
| 405 | /// '__finally' compound-statement |
| 406 | /// |
| 407 | StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) { |
| 408 | PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false), |
| 409 | raii2(Ident___abnormal_termination, false), |
| 410 | raii3(Ident_AbnormalTermination, false); |
| 411 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 412 | StmtResult Block(ParseCompoundStatement()); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 413 | if(Block.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 414 | return Block; |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 415 | |
| 416 | return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 419 | /// ParseLabeledStatement - We have an identifier and a ':' after it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 420 | /// |
| 421 | /// labeled-statement: |
| 422 | /// identifier ':' statement |
| 423 | /// [GNU] identifier ':' attributes[opt] statement |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 424 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 425 | StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 426 | assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && |
| 427 | "Not an identifier!"); |
| 428 | |
| 429 | Token IdentTok = Tok; // Save the whole token. |
| 430 | ConsumeToken(); // eat the identifier. |
| 431 | |
| 432 | assert(Tok.is(tok::colon) && "Not a label!"); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 433 | |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 434 | // identifier ':' statement |
| 435 | SourceLocation ColonLoc = ConsumeToken(); |
| 436 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 437 | // Read label attributes, if present. attrs will contain both C++11 and GNU |
| 438 | // attributes (if present) after this point. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 439 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 440 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 441 | StmtResult SubStmt(ParseStatement()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 442 | |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 443 | // 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] | 444 | if (SubStmt.isInvalid()) |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 445 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 447 | LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(), |
| 448 | IdentTok.getLocation()); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 449 | if (AttributeList *Attrs = attrs.getList()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 450 | Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 451 | attrs.clear(); |
| 452 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 454 | return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc, |
| 455 | SubStmt.get()); |
Argyrios Kyrtzidis | f7da726 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 456 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | |
| 458 | /// ParseCaseStatement |
| 459 | /// labeled-statement: |
| 460 | /// 'case' constant-expression ':' statement |
| 461 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
| 462 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 463 | StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) { |
Richard Smith | 46f1110 | 2011-04-21 22:48:40 +0000 | [diff] [blame] | 464 | assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 466 | // It is very very common for code to contain many case statements recursively |
| 467 | // nested, as in (but usually without indentation): |
| 468 | // case 1: |
| 469 | // case 2: |
| 470 | // case 3: |
| 471 | // case 4: |
| 472 | // case 5: etc. |
| 473 | // |
| 474 | // Parsing this naively works, but is both inefficient and can cause us to run |
| 475 | // out of stack space in our recursive descent parser. As a special case, |
Chris Lattner | 26140c6 | 2009-03-04 18:24:58 +0000 | [diff] [blame] | 476 | // flatten this recursion into an iterative loop. This is complex and gross, |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 477 | // but all the grossness is constrained to ParseCaseStatement (and some |
| 478 | // wierdness in the actions), so this is just local grossness :). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 480 | // TopLevelCase - This is the highest level we have parsed. 'case 1' in the |
| 481 | // example above. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 482 | StmtResult TopLevelCase(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 484 | // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which |
| 485 | // gets updated each time a new case is parsed, and whose body is unset so |
| 486 | // far. When parsing 'case 4', this is the 'case 3' node. |
Richard Trieu | b2fc690 | 2011-09-09 02:16:15 +0000 | [diff] [blame] | 487 | Stmt *DeepestParsedCaseStmt = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 489 | // While we have case statements, eat and stack them. |
David Majnemer | 0e1e69c | 2011-06-13 05:50:12 +0000 | [diff] [blame] | 490 | SourceLocation ColonLoc; |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 491 | do { |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 492 | SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() : |
| 493 | ConsumeToken(); // eat the 'case'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 495 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 496 | Actions.CodeCompleteCase(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 497 | cutOffParsing(); |
| 498 | return StmtError(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 499 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 6fb09c8 | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 501 | /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'. |
| 502 | /// Disable this form of error recovery while we're parsing the case |
| 503 | /// expression. |
| 504 | ColonProtectionRAIIObject ColonProtection(*this); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 505 | |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 506 | ExprResult LHS(MissingCase ? Expr : ParseConstantExpression()); |
| 507 | MissingCase = false; |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 508 | if (LHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | SkipUntil(tok::colon); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 510 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 513 | // GNU case range extension. |
| 514 | SourceLocation DotDotDotLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 515 | ExprResult RHS; |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 516 | if (Tok.is(tok::ellipsis)) { |
| 517 | Diag(Tok, diag::ext_gnu_case_range); |
| 518 | DotDotDotLoc = ConsumeToken(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 520 | RHS = ParseConstantExpression(); |
| 521 | if (RHS.isInvalid()) { |
| 522 | SkipUntil(tok::colon); |
| 523 | return StmtError(); |
| 524 | } |
| 525 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 6fb09c8 | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 527 | ColonProtection.restore(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 528 | |
John McCall | f6a3ab0 | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 529 | if (Tok.is(tok::colon)) { |
| 530 | ColonLoc = ConsumeToken(); |
| 531 | |
| 532 | // Treat "case blah;" as a typo for "case blah:". |
| 533 | } else if (Tok.is(tok::semi)) { |
| 534 | ColonLoc = ConsumeToken(); |
| 535 | Diag(ColonLoc, diag::err_expected_colon_after) << "'case'" |
| 536 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
| 537 | } else { |
Douglas Gregor | 662a482 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 538 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 539 | Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'" |
| 540 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
| 541 | ColonLoc = ExpectedLoc; |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 542 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 543 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 544 | StmtResult Case = |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 545 | Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc, |
| 546 | RHS.get(), ColonLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 548 | // If we had a sema error parsing this case, then just ignore it and |
| 549 | // continue parsing the sub-stmt. |
| 550 | if (Case.isInvalid()) { |
| 551 | if (TopLevelCase.isInvalid()) // No parsed case stmts. |
| 552 | return ParseStatement(); |
| 553 | // Otherwise, just don't add it as a nested case. |
| 554 | } else { |
| 555 | // If this is the first case statement we parsed, it becomes TopLevelCase. |
| 556 | // Otherwise we link it into the current chain. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 557 | Stmt *NextDeepest = Case.get(); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 558 | if (TopLevelCase.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 559 | TopLevelCase = Case; |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 560 | else |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 561 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get()); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 562 | DeepestParsedCaseStmt = NextDeepest; |
| 563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 565 | // Handle all case statements. |
| 566 | } while (Tok.is(tok::kw_case)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 568 | assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 570 | // If we found a non-case statement, start by parsing it. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 571 | StmtResult SubStmt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 573 | if (Tok.isNot(tok::r_brace)) { |
| 574 | SubStmt = ParseStatement(); |
| 575 | } else { |
| 576 | // Nicely diagnose the common error "switch (X) { case 4: }", which is |
| 577 | // not valid. |
David Majnemer | 63f04ab | 2011-06-14 15:24:38 +0000 | [diff] [blame] | 578 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
Richard Smith | 85b29a4 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 579 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
| 580 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 581 | SubStmt = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 584 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 585 | if (SubStmt.isInvalid()) |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 586 | SubStmt = Actions.ActOnNullStmt(SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 588 | // Install the body into the most deeply-nested case. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 589 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get()); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 591 | // Return the top level parsed statement tree. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 592 | return TopLevelCase; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /// ParseDefaultStatement |
| 596 | /// labeled-statement: |
| 597 | /// 'default' ':' statement |
| 598 | /// Note that this does not parse the 'statement' at the end. |
| 599 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 600 | StmtResult Parser::ParseDefaultStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 601 | assert(Tok.is(tok::kw_default) && "Not a default stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
| 603 | |
Douglas Gregor | 662a482 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 604 | SourceLocation ColonLoc; |
John McCall | f6a3ab0 | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 605 | if (Tok.is(tok::colon)) { |
| 606 | ColonLoc = ConsumeToken(); |
| 607 | |
| 608 | // Treat "default;" as a typo for "default:". |
| 609 | } else if (Tok.is(tok::semi)) { |
| 610 | ColonLoc = ConsumeToken(); |
| 611 | Diag(ColonLoc, diag::err_expected_colon_after) << "'default'" |
| 612 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
| 613 | } else { |
Douglas Gregor | 662a482 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 614 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 615 | Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'" |
| 616 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
| 617 | ColonLoc = ExpectedLoc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 619 | |
Richard Smith | 85b29a4 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 620 | StmtResult SubStmt; |
| 621 | |
| 622 | if (Tok.isNot(tok::r_brace)) { |
| 623 | SubStmt = ParseStatement(); |
| 624 | } else { |
| 625 | // Diagnose the common error "switch (X) {... default: }", which is |
| 626 | // not valid. |
David Majnemer | 63f04ab | 2011-06-14 15:24:38 +0000 | [diff] [blame] | 627 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
Richard Smith | 85b29a4 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 628 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
| 629 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
| 630 | SubStmt = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Richard Smith | 85b29a4 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 633 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 634 | if (SubStmt.isInvalid()) |
Richard Smith | 85b29a4 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 635 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 636 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 637 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 638 | SubStmt.get(), getCurScope()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 641 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
| 642 | return ParseCompoundStatement(isStmtExpr, Scope::DeclScope); |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 643 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 644 | |
| 645 | /// ParseCompoundStatement - Parse a "{}" block. |
| 646 | /// |
| 647 | /// compound-statement: [C99 6.8.2] |
| 648 | /// { block-item-list[opt] } |
| 649 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 650 | /// |
| 651 | /// block-item-list: |
| 652 | /// block-item |
| 653 | /// block-item-list block-item |
| 654 | /// |
| 655 | /// block-item: |
| 656 | /// declaration |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 657 | /// [GNU] '__extension__' declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 658 | /// statement |
| 659 | /// [OMP] openmp-directive [TODO] |
| 660 | /// |
| 661 | /// [GNU] label-declarations: |
| 662 | /// [GNU] label-declaration |
| 663 | /// [GNU] label-declarations label-declaration |
| 664 | /// |
| 665 | /// [GNU] label-declaration: |
| 666 | /// [GNU] '__label__' identifier-list ';' |
| 667 | /// |
| 668 | /// [OMP] openmp-directive: [TODO] |
| 669 | /// [OMP] barrier-directive |
| 670 | /// [OMP] flush-directive |
| 671 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 672 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 673 | unsigned ScopeFlags) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 674 | assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 676 | // Enter a scope to hold everything within the compound stmt. Compound |
| 677 | // statements can always hold declarations. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 678 | ParseScope CompoundScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 679 | |
| 680 | // Parse the statements in the body. |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 681 | return ParseCompoundStatementBody(isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 685 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 686 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 687 | /// stack. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 688 | StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 690 | Tok.getLocation(), |
| 691 | "in compound statement ('{}')"); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 692 | |
| 693 | // Record the state of the FP_CONTRACT pragma, restore on leaving the |
| 694 | // compound statement. |
| 695 | Sema::FPContractStateRAII SaveFPContractState(Actions); |
| 696 | |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 697 | InMessageExpressionRAIIObject InMessage(*this, false); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 698 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 699 | if (T.consumeOpen()) |
| 700 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 702 | Sema::CompoundScopeRAII CompoundScope(Actions); |
| 703 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 704 | StmtVector Stmts; |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 706 | // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are |
| 707 | // only allowed at the start of a compound stmt regardless of the language. |
| 708 | while (Tok.is(tok::kw___label__)) { |
| 709 | SourceLocation LabelLoc = ConsumeToken(); |
| 710 | Diag(LabelLoc, diag::ext_gnu_local_label); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 712 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 713 | while (1) { |
| 714 | if (Tok.isNot(tok::identifier)) { |
| 715 | Diag(Tok, diag::err_expected_ident); |
| 716 | break; |
| 717 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 719 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 720 | SourceLocation IdLoc = ConsumeToken(); |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 721 | DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc)); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 723 | if (!Tok.is(tok::comma)) |
| 724 | break; |
| 725 | ConsumeToken(); |
| 726 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 727 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 728 | DeclSpec DS(AttrFactory); |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 729 | DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
| 730 | DeclsInGroup.data(), DeclsInGroup.size()); |
| 731 | StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 732 | |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 733 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 734 | if (R.isUsable()) |
| 735 | Stmts.push_back(R.release()); |
| 736 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 738 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 739 | if (Tok.is(tok::annot_pragma_unused)) { |
| 740 | HandlePragmaUnused(); |
| 741 | continue; |
| 742 | } |
| 743 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 744 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 745 | Tok.is(tok::kw___if_not_exists))) { |
| 746 | ParseMicrosoftIfExistsStatement(Stmts); |
| 747 | continue; |
| 748 | } |
| 749 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 750 | StmtResult R; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 751 | if (Tok.isNot(tok::kw___extension__)) { |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 752 | R = ParseStatementOrDeclaration(Stmts, false); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 753 | } else { |
| 754 | // __extension__ can start declarations and it can also be a unary |
| 755 | // operator for expressions. Consume multiple __extension__ markers here |
| 756 | // until we can determine which is which. |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 757 | // FIXME: This loses extension expressions in the AST! |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 758 | SourceLocation ExtLoc = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 759 | while (Tok.is(tok::kw___extension__)) |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 760 | ConsumeToken(); |
Chris Lattner | 39146d6 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 761 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 762 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 763 | MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 765 | // If this is the start of a declaration, parse it as such. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 766 | if (isDeclarationStatement()) { |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 767 | // __extension__ silences extension warnings in the subdeclaration. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 768 | // FIXME: Save the __extension__ on the decl as a node somehow? |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 769 | ExtensionRAIIObject O(Diags); |
| 770 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 771 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 772 | DeclGroupPtrTy Res = ParseDeclaration(Stmts, |
| 773 | Declarator::BlockContext, DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 774 | attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 775 | R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 776 | } else { |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 777 | // Otherwise this was a unary __extension__ marker. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 778 | ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); |
Chris Lattner | 043a0b5 | 2008-03-13 06:32:11 +0000 | [diff] [blame] | 779 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 780 | if (Res.isInvalid()) { |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 781 | SkipUntil(tok::semi); |
| 782 | continue; |
| 783 | } |
Sebastian Redl | f512e82 | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 784 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 785 | // FIXME: Use attributes? |
Chris Lattner | 39146d6 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 786 | // Eat the semicolon at the end of stmt and convert the expr into a |
| 787 | // statement. |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 788 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 789 | R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get())); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 792 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 793 | if (R.isUsable()) |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 794 | Stmts.push_back(R.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 796 | |
Argyrios Kyrtzidis | 5d5ed59 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 797 | SourceLocation CloseLoc = Tok.getLocation(); |
| 798 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | // 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] | 800 | if (Tok.isNot(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | Diag(Tok, diag::err_expected_rbrace); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 802 | Diag(T.getOpenLocation(), diag::note_matching) << "{"; |
Argyrios Kyrtzidis | 5d5ed59 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 803 | // Recover by creating a compound statement with what we parsed so far, |
| 804 | // instead of dropping everything and returning StmtError(); |
| 805 | } else { |
| 806 | if (!T.consumeClose()) |
| 807 | CloseLoc = T.getCloseLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 808 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 809 | |
Argyrios Kyrtzidis | 5d5ed59 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 810 | return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 811 | Stmts, isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 814 | /// ParseParenExprOrCondition: |
| 815 | /// [C ] '(' expression ')' |
Chris Lattner | ff871fb | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 816 | /// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true] |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 817 | /// |
| 818 | /// This function parses and performs error recovery on the specified condition |
| 819 | /// or expression (depending on whether we're in C++ or C mode). This function |
| 820 | /// goes out of its way to recover well. It returns true if there was a parser |
| 821 | /// error (the right paren couldn't be found), which indicates that the caller |
| 822 | /// should try to recover harder. It returns false if the condition is |
| 823 | /// successfully parsed. Note that a successful parse can still have semantic |
| 824 | /// errors in the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 825 | bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 826 | Decl *&DeclResult, |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 827 | SourceLocation Loc, |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 828 | bool ConvertToBoolean) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 829 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 830 | T.consumeOpen(); |
| 831 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 832 | if (getLangOpts().CPlusPlus) |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 833 | ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 834 | else { |
| 835 | ExprResult = ParseExpression(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 836 | DeclResult = 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 838 | // If required, convert to a boolean value. |
| 839 | if (!ExprResult.isInvalid() && ConvertToBoolean) |
| 840 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 841 | = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get()); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 844 | // If the parser was confused by the condition and we don't have a ')', try to |
| 845 | // recover by skipping ahead to a semi and bailing out. If condexp is |
| 846 | // semantically invalid but we have well formed code, keep going. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 847 | if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) { |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 848 | SkipUntil(tok::semi); |
| 849 | // Skipping may have stopped if it found the containing ')'. If so, we can |
| 850 | // continue parsing the if statement. |
| 851 | if (Tok.isNot(tok::r_paren)) |
| 852 | return true; |
| 853 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 855 | // Otherwise the condition is valid or the rparen is present. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 856 | T.consumeClose(); |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 857 | |
Chris Lattner | bddc7e5 | 2012-04-28 16:24:20 +0000 | [diff] [blame] | 858 | // Check for extraneous ')'s to catch things like "if (foo())) {". We know |
| 859 | // that all callers are looking for a statement after the condition, so ")" |
| 860 | // isn't valid. |
| 861 | while (Tok.is(tok::r_paren)) { |
| 862 | Diag(Tok, diag::err_extraneous_rparen_in_condition) |
| 863 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 864 | ConsumeParen(); |
| 865 | } |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 866 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | |
| 870 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 871 | /// ParseIfStatement |
| 872 | /// if-statement: [C99 6.8.4.1] |
| 873 | /// 'if' '(' expression ')' statement |
| 874 | /// 'if' '(' expression ')' statement 'else' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 875 | /// [C++] 'if' '(' condition ')' statement |
| 876 | /// [C++] 'if' '(' condition ')' statement 'else' statement |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 877 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 878 | StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 879 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 880 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
| 881 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 882 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 883 | Diag(Tok, diag::err_expected_lparen_after) << "if"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | SkipUntil(tok::semi); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 885 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | } |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 887 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 888 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 889 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 890 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 891 | // the case for C90. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 892 | // |
| 893 | // C++ 6.4p3: |
| 894 | // A name introduced by a declaration in a condition is in scope from its |
| 895 | // point of declaration until the end of the substatements controlled by the |
| 896 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 897 | // C++ 3.3.2p4: |
| 898 | // Names declared in the for-init-statement, and in the condition of if, |
| 899 | // while, for, and switch statements are local to the if, while, for, or |
| 900 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 901 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 902 | ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 903 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | // Parse the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 905 | ExprResult CondExp; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 906 | Decl *CondVar = 0; |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 907 | if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true)) |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 908 | return StmtError(); |
Chris Lattner | 18914bc | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 909 | |
David Blaikie | def0762 | 2012-05-16 04:20:04 +0000 | [diff] [blame] | 910 | FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 912 | // 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] | 913 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 914 | // 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] | 915 | // |
| 916 | // C++ 6.4p1: |
| 917 | // The substatement in a selection-statement (each substatement, in the else |
| 918 | // form of the if statement) implicitly defines a local scope. |
| 919 | // |
| 920 | // For C++ we create a scope for the condition and a new scope for |
| 921 | // substatements because: |
| 922 | // -When the 'then' scope exits, we want the condition declaration to still be |
| 923 | // active for the 'else' scope too. |
| 924 | // -Sema will detect name clashes by considering declarations of a |
| 925 | // 'ControlScope' as part of its direct subscope. |
| 926 | // -If we wanted the condition and substatement to be in the same scope, we |
| 927 | // would have to notify ParseStatement not to create a new scope. It's |
| 928 | // simpler to let it create a new scope. |
| 929 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 931 | C99orCXX && Tok.isNot(tok::l_brace)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 932 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 933 | // Read the 'then' stmt. |
| 934 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 935 | |
| 936 | SourceLocation InnerStatementTrailingElseLoc; |
| 937 | StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 938 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 939 | // Pop the 'if' scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 940 | InnerScope.Exit(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 941 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | // If it has an else, parse it. |
| 943 | SourceLocation ElseLoc; |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 944 | SourceLocation ElseStmtLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 945 | StmtResult ElseStmt; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 947 | if (Tok.is(tok::kw_else)) { |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 948 | if (TrailingElseLoc) |
| 949 | *TrailingElseLoc = Tok.getLocation(); |
| 950 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | ElseLoc = ConsumeToken(); |
Chris Lattner | 966c78b | 2010-04-12 06:12:50 +0000 | [diff] [blame] | 952 | ElseStmtLoc = Tok.getLocation(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 953 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 954 | // 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] | 955 | // there is no compound stmt. C90 does not have this clause. We only do |
| 956 | // this if the body isn't a compound statement to avoid push/pop in common |
| 957 | // cases. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 958 | // |
| 959 | // C++ 6.4p1: |
| 960 | // The substatement in a selection-statement (each substatement, in the else |
| 961 | // form of the if statement) implicitly defines a local scope. |
| 962 | // |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 963 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 964 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 965 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | ElseStmt = ParseStatement(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 967 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 968 | // Pop the 'else' scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 969 | InnerScope.Exit(); |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 970 | } else if (Tok.is(tok::code_completion)) { |
| 971 | Actions.CodeCompleteAfterIf(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 972 | cutOffParsing(); |
| 973 | return StmtError(); |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 974 | } else if (InnerStatementTrailingElseLoc.isValid()) { |
| 975 | Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 976 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 977 | |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 978 | IfScope.Exit(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | 18914bc | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 980 | // If the condition was invalid, discard the if statement. We could recover |
| 981 | // better by replacing it with a valid expr, but don't do that yet. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 982 | if (CondExp.isInvalid() && !CondVar) |
Chris Lattner | 18914bc | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 983 | return StmtError(); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 984 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 985 | // If the then or else stmt is invalid and the other is valid (and present), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | // make turn the invalid one into a null stmt to avoid dropping the other |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 987 | // part. If both are invalid, return error. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 988 | if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || |
| 989 | (ThenStmt.isInvalid() && ElseStmt.get() == 0) || |
| 990 | (ThenStmt.get() == 0 && ElseStmt.isInvalid())) { |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 991 | // 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] | 992 | return StmtError(); |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 993 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 994 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 995 | // Now if either are invalid, replace with a ';'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 996 | if (ThenStmt.isInvalid()) |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 997 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 998 | if (ElseStmt.isInvalid()) |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 999 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1000 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1001 | return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(), |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 1002 | ElseLoc, ElseStmt.get()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /// ParseSwitchStatement |
| 1006 | /// switch-statement: |
| 1007 | /// 'switch' '(' expression ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1008 | /// [C++] 'switch' '(' condition ')' statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1009 | StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1010 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1011 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
| 1012 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1013 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1014 | Diag(Tok, diag::err_expected_lparen_after) << "switch"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | SkipUntil(tok::semi); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1016 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1017 | } |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1018 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1019 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1021 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 1022 | // not the case for C90. Start the switch scope. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1023 | // |
| 1024 | // C++ 6.4p3: |
| 1025 | // A name introduced by a declaration in a condition is in scope from its |
| 1026 | // point of declaration until the end of the substatements controlled by the |
| 1027 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1028 | // C++ 3.3.2p4: |
| 1029 | // Names declared in the for-init-statement, and in the condition of if, |
| 1030 | // while, for, and switch statements are local to the if, while, for, or |
| 1031 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1032 | // |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 1033 | unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope; |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1034 | if (C99orCXX) |
| 1035 | ScopeFlags |= Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1036 | ParseScope SwitchScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Parse the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1039 | ExprResult Cond; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1040 | Decl *CondVar = 0; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1041 | if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false)) |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1042 | return StmtError(); |
Eli Friedman | 2342ef7 | 2008-12-17 22:19:57 +0000 | [diff] [blame] | 1043 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1044 | StmtResult Switch |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1045 | = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1047 | if (Switch.isInvalid()) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1048 | // Skip the switch body. |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1049 | // FIXME: This is not optimal recovery, but parsing the body is more |
| 1050 | // dangerous due to the presence of case and default statements, which |
| 1051 | // will have no place to connect back with the switch. |
Douglas Gregor | 4186ff4 | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 1052 | if (Tok.is(tok::l_brace)) { |
| 1053 | ConsumeBrace(); |
| 1054 | SkipUntil(tok::r_brace, false, false); |
| 1055 | } else |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1056 | SkipUntil(tok::semi); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1057 | return Switch; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1058 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1060 | // 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] | 1061 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1062 | // 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] | 1063 | // |
| 1064 | // C++ 6.4p1: |
| 1065 | // The substatement in a selection-statement (each substatement, in the else |
| 1066 | // form of the if statement) implicitly defines a local scope. |
| 1067 | // |
| 1068 | // See comments in ParseIfStatement for why we create a scope for the |
| 1069 | // condition and a new scope for substatement in C++. |
| 1070 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1072 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1073 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | // Read the body statement. |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1075 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | |
Chris Lattner | 7e52de4 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1077 | // Pop the scopes. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1078 | InnerScope.Exit(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1079 | SwitchScope.Exit(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1080 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1081 | if (Body.isInvalid()) { |
Chris Lattner | 7e52de4 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1082 | // FIXME: Remove the case statement list from the Switch statement. |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Put the synthesized null statement on the same line as the end of switch |
| 1085 | // condition. |
| 1086 | SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd(); |
| 1087 | Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation); |
| 1088 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1089 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1090 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /// ParseWhileStatement |
| 1094 | /// while-statement: [C99 6.8.5.1] |
| 1095 | /// 'while' '(' expression ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1096 | /// [C++] 'while' '(' condition ')' statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1097 | StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1098 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | SourceLocation WhileLoc = Tok.getLocation(); |
| 1100 | ConsumeToken(); // eat the 'while'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1102 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1103 | Diag(Tok, diag::err_expected_lparen_after) << "while"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1104 | SkipUntil(tok::semi); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1105 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1106 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1107 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1108 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1110 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 1111 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1112 | // |
| 1113 | // C++ 6.4p3: |
| 1114 | // A name introduced by a declaration in a condition is in scope from its |
| 1115 | // point of declaration until the end of the substatements controlled by the |
| 1116 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1117 | // C++ 3.3.2p4: |
| 1118 | // Names declared in the for-init-statement, and in the condition of if, |
| 1119 | // while, for, and switch statements are local to the if, while, for, or |
| 1120 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1121 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1122 | unsigned ScopeFlags; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1123 | if (C99orCXX) |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1124 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 1125 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1126 | else |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1127 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 1128 | ParseScope WhileScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Parse the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1131 | ExprResult Cond; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1132 | Decl *CondVar = 0; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1133 | if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true)) |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1134 | return StmtError(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1135 | |
David Blaikie | def0762 | 2012-05-16 04:20:04 +0000 | [diff] [blame] | 1136 | FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1138 | // 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] | 1139 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1140 | // 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] | 1141 | // |
| 1142 | // C++ 6.5p2: |
| 1143 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1144 | // which is entered and exited each time through the loop. |
| 1145 | // |
| 1146 | // See comments in ParseIfStatement for why we create a scope for the |
| 1147 | // condition and a new scope for substatement in C++. |
| 1148 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1150 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1151 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | // Read the body statement. |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1153 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1155 | // Pop the body scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1156 | InnerScope.Exit(); |
| 1157 | WhileScope.Exit(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1158 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1159 | if ((Cond.isInvalid() && !CondVar) || Body.isInvalid()) |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1160 | return StmtError(); |
| 1161 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1162 | return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /// ParseDoStatement |
| 1166 | /// do-statement: [C99 6.8.5.2] |
| 1167 | /// 'do' statement 'while' '(' expression ')' ';' |
| 1168 | /// Note: this lets the caller parse the end ';'. |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1169 | StmtResult Parser::ParseDoStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1170 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1171 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1173 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 1174 | // the case for C90. Start the loop scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1175 | unsigned ScopeFlags; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1176 | if (getLangOpts().C99) |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1177 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1178 | else |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1179 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1181 | ParseScope DoScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1183 | // 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] | 1184 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1185 | // 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] | 1186 | // |
| 1187 | // C++ 6.5p2: |
| 1188 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1189 | // which is entered and exited each time through the loop. |
| 1190 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1191 | ParseScope InnerScope(this, Scope::DeclScope, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1192 | (getLangOpts().C99 || getLangOpts().CPlusPlus) && |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1193 | Tok.isNot(tok::l_brace)); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1195 | // Read the body statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1196 | StmtResult Body(ParseStatement()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1198 | // Pop the body scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1199 | InnerScope.Exit(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1201 | if (Tok.isNot(tok::kw_while)) { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1202 | if (!Body.isInvalid()) { |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1203 | Diag(Tok, diag::err_expected_while); |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 1204 | Diag(DoLoc, diag::note_matching) << "do"; |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1205 | SkipUntil(tok::semi, false, true); |
| 1206 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1207 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1208 | } |
| 1209 | SourceLocation WhileLoc = ConsumeToken(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1210 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1211 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1212 | Diag(Tok, diag::err_expected_lparen_after) << "do/while"; |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1213 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1214 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1215 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | ff871fb | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 1217 | // Parse the parenthesized condition. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1218 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1219 | T.consumeOpen(); |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1220 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1221 | // FIXME: Do not just parse the attribute contents and throw them away |
| 1222 | ParsedAttributesWithRange attrs(AttrFactory); |
| 1223 | MaybeParseCXX0XAttributes(attrs); |
| 1224 | ProhibitAttributes(attrs); |
| 1225 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1226 | ExprResult Cond = ParseExpression(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1227 | T.consumeClose(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1228 | DoScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1229 | |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1230 | if (Cond.isInvalid() || Body.isInvalid()) |
| 1231 | return StmtError(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1232 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1233 | return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(), |
| 1234 | Cond.get(), T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /// ParseForStatement |
| 1238 | /// for-statement: [C99 6.8.5.3] |
| 1239 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 1240 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1241 | /// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')' |
| 1242 | /// [C++] statement |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1243 | /// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1244 | /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement |
| 1245 | /// [OBJC2] 'for' '(' expr 'in' expr ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1246 | /// |
| 1247 | /// [C++] for-init-statement: |
| 1248 | /// [C++] expression-statement |
| 1249 | /// [C++] simple-declaration |
| 1250 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1251 | /// [C++0x] for-range-declaration: |
| 1252 | /// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1253 | /// [C++0x] for-range-initializer: |
| 1254 | /// [C++0x] expression |
| 1255 | /// [C++0x] braced-init-list [TODO] |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1256 | StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1257 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1260 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1261 | Diag(Tok, diag::err_expected_lparen_after) << "for"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | SkipUntil(tok::semi); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1263 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1264 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1265 | |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1266 | bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || |
| 1267 | getLangOpts().ObjC1; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1269 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 1270 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1271 | // |
| 1272 | // C++ 6.4p3: |
| 1273 | // A name introduced by a declaration in a condition is in scope from its |
| 1274 | // point of declaration until the end of the substatements controlled by the |
| 1275 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1276 | // C++ 3.3.2p4: |
| 1277 | // Names declared in the for-init-statement, and in the condition of if, |
| 1278 | // while, for, and switch statements are local to the if, while, for, or |
| 1279 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1280 | // C++ 6.5.3p1: |
| 1281 | // Names declared in the for-init-statement are in the same declarative-region |
| 1282 | // as those declared in the condition. |
| 1283 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1284 | unsigned ScopeFlags; |
Chris Lattner | 4d00f2a | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1285 | if (C99orCXXorObjC) |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1286 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 1287 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1288 | else |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1289 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 1290 | |
| 1291 | ParseScope ForScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1292 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1293 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1294 | T.consumeOpen(); |
| 1295 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1296 | ExprResult Value; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1297 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1298 | bool ForEach = false, ForRange = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1299 | StmtResult FirstPart; |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1300 | bool SecondPartIsInvalid = false; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1301 | FullExprArg SecondPart(Actions); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1302 | ExprResult Collection; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1303 | ForRangeInit ForRangeInit; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1304 | FullExprArg ThirdPart(Actions); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1305 | Decl *SecondVar = 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1306 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1307 | if (Tok.is(tok::code_completion)) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1308 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1309 | C99orCXXorObjC? Sema::PCC_ForInit |
| 1310 | : Sema::PCC_Expression); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1311 | cutOffParsing(); |
| 1312 | return StmtError(); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1313 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1314 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1315 | ParsedAttributesWithRange attrs(AttrFactory); |
| 1316 | MaybeParseCXX0XAttributes(attrs); |
| 1317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | // Parse the first part of the for specifier. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1319 | if (Tok.is(tok::semi)) { // for (; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1320 | ProhibitAttributes(attrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1321 | // no first part, eat the ';'. |
| 1322 | ConsumeToken(); |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 1323 | } else if (isForInitDeclaration()) { // for (int X = 4; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | // Parse declaration, which eats the ';'. |
Chris Lattner | 4d00f2a | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1325 | if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1326 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1327 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1328 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1329 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1330 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1331 | // In C++0x, "for (T NS:a" might not be a typo for :: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1332 | bool MightBeForRangeStmt = getLangOpts().CPlusPlus; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1333 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
| 1334 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1335 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1336 | StmtVector Stmts; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1337 | DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1338 | DeclEnd, attrs, false, |
| 1339 | MightBeForRangeStmt ? |
| 1340 | &ForRangeInit : 0); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1341 | FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1343 | if (ForRangeInit.ParsedForRangeDecl()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1344 | Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1345 | diag::warn_cxx98_compat_for_range : diag::ext_for_range); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 1346 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1347 | ForRange = true; |
| 1348 | } else if (Tok.is(tok::semi)) { // for (int x = 4; |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1349 | ConsumeToken(); |
| 1350 | } else if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 1351 | Actions.ActOnForEachDeclStmt(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | // ObjC: for (id x in expr) |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1353 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1354 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1355 | if (Tok.is(tok::code_completion)) { |
| 1356 | Actions.CodeCompleteObjCForCollection(getCurScope(), DG); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1357 | cutOffParsing(); |
| 1358 | return StmtError(); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1359 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1360 | Collection = ParseExpression(); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1361 | } else { |
| 1362 | Diag(Tok, diag::err_expected_semi_for); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1363 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1364 | } else { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1365 | ProhibitAttributes(attrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1366 | Value = ParseExpression(); |
| 1367 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1368 | ForEach = isTokIdentifier_in(); |
| 1369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | // Turn the expression into a stmt. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1371 | if (!Value.isInvalid()) { |
| 1372 | if (ForEach) |
| 1373 | FirstPart = Actions.ActOnForEachLValueExpr(Value.get()); |
| 1374 | else |
| 1375 | FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get())); |
| 1376 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1378 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1379 | ConsumeToken(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1380 | } else if (ForEach) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1381 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1382 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1383 | if (Tok.is(tok::code_completion)) { |
| 1384 | Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1385 | cutOffParsing(); |
| 1386 | return StmtError(); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1387 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1388 | Collection = ParseExpression(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1389 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) { |
Richard Smith | a44854a | 2011-12-20 22:56:20 +0000 | [diff] [blame] | 1390 | // User tried to write the reasonable, but ill-formed, for-range-statement |
| 1391 | // for (expr : expr) { ... } |
| 1392 | Diag(Tok, diag::err_for_range_expected_decl) |
| 1393 | << FirstPart.get()->getSourceRange(); |
| 1394 | SkipUntil(tok::r_paren, false, true); |
| 1395 | SecondPartIsInvalid = true; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1396 | } else { |
Douglas Gregor | b72c778 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1397 | if (!Value.isInvalid()) { |
| 1398 | Diag(Tok, diag::err_expected_semi_for); |
| 1399 | } else { |
| 1400 | // Skip until semicolon or rparen, don't consume it. |
| 1401 | SkipUntil(tok::r_paren, true, true); |
| 1402 | if (Tok.is(tok::semi)) |
| 1403 | ConsumeToken(); |
| 1404 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1407 | if (!ForEach && !ForRange) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1408 | assert(!SecondPart.get() && "Shouldn't have a second expression yet."); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1409 | // Parse the second part of the for specifier. |
| 1410 | if (Tok.is(tok::semi)) { // for (...;; |
| 1411 | // no second part. |
Douglas Gregor | b72c778 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1412 | } else if (Tok.is(tok::r_paren)) { |
| 1413 | // missing both semicolons. |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1414 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1415 | ExprResult Second; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1416 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1417 | ParseCXXCondition(Second, SecondVar, ForLoc, true); |
| 1418 | else { |
| 1419 | Second = ParseExpression(); |
| 1420 | if (!Second.isInvalid()) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1421 | Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1422 | Second.get()); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1423 | } |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1424 | SecondPartIsInvalid = Second.isInvalid(); |
David Blaikie | def0762 | 2012-05-16 04:20:04 +0000 | [diff] [blame] | 1425 | SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1426 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1427 | |
Douglas Gregor | b72c778 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1428 | if (Tok.isNot(tok::semi)) { |
| 1429 | if (!SecondPartIsInvalid || SecondVar) |
| 1430 | Diag(Tok, diag::err_expected_semi_for); |
| 1431 | else |
| 1432 | // Skip until semicolon or rparen, don't consume it. |
| 1433 | SkipUntil(tok::r_paren, true, true); |
| 1434 | } |
| 1435 | |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1436 | if (Tok.is(tok::semi)) { |
| 1437 | ConsumeToken(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1438 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1439 | |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1440 | // Parse the third part of the for specifier. |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1441 | if (Tok.isNot(tok::r_paren)) { // for (...;...;) |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1442 | ExprResult Third = ParseExpression(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1443 | ThirdPart = Actions.MakeFullExpr(Third.take()); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1444 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1445 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1446 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1447 | T.consumeClose(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1448 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1449 | // We need to perform most of the semantic analysis for a C++0x for-range |
| 1450 | // statememt before parsing the body, in order to be able to deduce the type |
| 1451 | // of an auto-typed loop variable. |
| 1452 | StmtResult ForRangeStmt; |
Fariborz Jahanian | a1eec4b | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1453 | StmtResult ForEachStmt; |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1454 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1455 | if (ForRange) { |
Sam Panzer | bc20bbb | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1456 | ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(), |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1457 | ForRangeInit.ColonLoc, |
| 1458 | ForRangeInit.RangeExpr.get(), |
Richard Smith | 8b533d9 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 1459 | T.getCloseLocation(), |
| 1460 | Sema::BFRK_Build); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1461 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Similarly, we need to do the semantic analysis for a for-range |
| 1464 | // statement immediately in order to close over temporaries correctly. |
| 1465 | } else if (ForEach) { |
Sam Panzer | bc20bbb | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1466 | ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc, |
Fariborz Jahanian | a1eec4b | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1467 | FirstPart.take(), |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1468 | Collection.take(), |
Fariborz Jahanian | a1eec4b | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1469 | T.getCloseLocation()); |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1472 | // 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] | 1473 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1474 | // 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] | 1475 | // |
| 1476 | // C++ 6.5p2: |
| 1477 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1478 | // which is entered and exited each time through the loop. |
| 1479 | // |
| 1480 | // See comments in ParseIfStatement for why we create a scope for |
| 1481 | // for-init-statement/condition and a new scope for substatement in C++. |
| 1482 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | ParseScope InnerScope(this, Scope::DeclScope, |
Chris Lattner | 4d00f2a | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1484 | C99orCXXorObjC && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | // Read the body statement. |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1487 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1489 | // Pop the body scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1490 | InnerScope.Exit(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1491 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1492 | // Leave the for-scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1493 | ForScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1494 | |
| 1495 | if (Body.isInvalid()) |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1496 | return StmtError(); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1497 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1498 | if (ForEach) |
Fariborz Jahanian | a1eec4b | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1499 | return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(), |
| 1500 | Body.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1502 | if (ForRange) |
| 1503 | return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take()); |
| 1504 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1505 | return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(), |
| 1506 | SecondPart, SecondVar, ThirdPart, |
| 1507 | T.getCloseLocation(), Body.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /// ParseGotoStatement |
| 1511 | /// jump-statement: |
| 1512 | /// 'goto' identifier ';' |
| 1513 | /// [GNU] 'goto' '*' expression ';' |
| 1514 | /// |
| 1515 | /// Note: this lets the caller parse the end ';'. |
| 1516 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1517 | StmtResult Parser::ParseGotoStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1518 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1519 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1520 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1521 | StmtResult Res; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1522 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1523 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
| 1524 | Tok.getLocation()); |
| 1525 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1526 | ConsumeToken(); |
Eli Friedman | f01fdff | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 1527 | } else if (Tok.is(tok::star)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | // GNU indirect goto extension. |
| 1529 | Diag(Tok, diag::ext_gnu_indirect_goto); |
| 1530 | SourceLocation StarLoc = ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1531 | ExprResult R(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1532 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1533 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1534 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1535 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1536 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take()); |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 1537 | } else { |
| 1538 | Diag(Tok, diag::err_expected_ident); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1539 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1540 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1541 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1542 | return Res; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /// ParseContinueStatement |
| 1546 | /// jump-statement: |
| 1547 | /// 'continue' ';' |
| 1548 | /// |
| 1549 | /// Note: this lets the caller parse the end ';'. |
| 1550 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1551 | StmtResult Parser::ParseContinueStatement() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1552 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1553 | return Actions.ActOnContinueStmt(ContinueLoc, getCurScope()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | /// ParseBreakStatement |
| 1557 | /// jump-statement: |
| 1558 | /// 'break' ';' |
| 1559 | /// |
| 1560 | /// Note: this lets the caller parse the end ';'. |
| 1561 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1562 | StmtResult Parser::ParseBreakStatement() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1563 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1564 | return Actions.ActOnBreakStmt(BreakLoc, getCurScope()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /// ParseReturnStatement |
| 1568 | /// jump-statement: |
| 1569 | /// 'return' expression[opt] ';' |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1570 | StmtResult Parser::ParseReturnStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1571 | assert(Tok.is(tok::kw_return) && "Not a return stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1572 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1573 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1574 | ExprResult R; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1575 | if (Tok.isNot(tok::semi)) { |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1576 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1577 | Actions.CodeCompleteReturn(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1578 | cutOffParsing(); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1579 | return StmtError(); |
| 1580 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1581 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1582 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) { |
Douglas Gregor | 6f4596c | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1583 | R = ParseInitializer(); |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1584 | if (R.isUsable()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1585 | Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1586 | diag::warn_cxx98_compat_generalized_initializer_lists : |
| 1587 | diag::ext_generalized_initializer_lists) |
Douglas Gregor | 6f4596c | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1588 | << R.get()->getSourceRange(); |
| 1589 | } else |
| 1590 | R = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1591 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1592 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1593 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | } |
| 1595 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1596 | return Actions.ActOnReturnStmt(ReturnLoc, R.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1599 | /// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled, |
| 1600 | /// this routine is called to collect the tokens for an MS asm statement. |
Chad Rosier | 8cd64b4 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 1601 | /// |
| 1602 | /// [MS] ms-asm-statement: |
| 1603 | /// ms-asm-block |
| 1604 | /// ms-asm-block ms-asm-statement |
| 1605 | /// |
| 1606 | /// [MS] ms-asm-block: |
| 1607 | /// '__asm' ms-asm-line '\n' |
| 1608 | /// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt] |
| 1609 | /// |
| 1610 | /// [MS] ms-asm-instruction-block |
| 1611 | /// ms-asm-line |
| 1612 | /// ms-asm-line '\n' ms-asm-instruction-block |
| 1613 | /// |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1614 | StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) { |
Chad Rosier | 56d7f23 | 2012-08-24 21:42:51 +0000 | [diff] [blame] | 1615 | // MS-style inline assembly is not fully supported, so emit a warning. |
| 1616 | Diag(AsmLoc, diag::warn_unsupported_msasm); |
| 1617 | |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1618 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 1619 | SourceLocation EndLoc = AsmLoc; |
Chad Rosier | 8cd64b4 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 1620 | SmallVector<Token, 4> AsmToks; |
Chad Rosier | 21ef711 | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 1621 | |
| 1622 | bool InBraces = false; |
| 1623 | unsigned short savedBraceCount = 0; |
| 1624 | bool InAsmComment = false; |
| 1625 | FileID FID; |
| 1626 | unsigned LineNo = 0; |
| 1627 | unsigned NumTokensRead = 0; |
| 1628 | SourceLocation LBraceLoc; |
| 1629 | |
| 1630 | if (Tok.is(tok::l_brace)) { |
| 1631 | // Braced inline asm: consume the opening brace. |
| 1632 | InBraces = true; |
| 1633 | savedBraceCount = BraceCount; |
| 1634 | EndLoc = LBraceLoc = ConsumeBrace(); |
| 1635 | ++NumTokensRead; |
| 1636 | } else { |
| 1637 | // Single-line inline asm; compute which line it is on. |
| 1638 | std::pair<FileID, unsigned> ExpAsmLoc = |
| 1639 | SrcMgr.getDecomposedExpansionLoc(EndLoc); |
| 1640 | FID = ExpAsmLoc.first; |
| 1641 | LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second); |
| 1642 | } |
| 1643 | |
| 1644 | SourceLocation TokLoc = Tok.getLocation(); |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1645 | do { |
Chad Rosier | 21ef711 | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 1646 | // If we hit EOF, we're done, period. |
| 1647 | if (Tok.is(tok::eof)) |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1648 | break; |
Chad Rosier | 21ef711 | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 1649 | |
Chad Rosier | 21ef711 | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 1650 | if (!InAsmComment && Tok.is(tok::semi)) { |
| 1651 | // A semicolon in an asm is the start of a comment. |
| 1652 | InAsmComment = true; |
| 1653 | if (InBraces) { |
| 1654 | // Compute which line the comment is on. |
| 1655 | std::pair<FileID, unsigned> ExpSemiLoc = |
| 1656 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
| 1657 | FID = ExpSemiLoc.first; |
| 1658 | LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second); |
| 1659 | } |
| 1660 | } else if (!InBraces || InAsmComment) { |
| 1661 | // If end-of-line is significant, check whether this token is on a |
| 1662 | // new line. |
| 1663 | std::pair<FileID, unsigned> ExpLoc = |
| 1664 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
| 1665 | if (ExpLoc.first != FID || |
| 1666 | SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) { |
| 1667 | // If this is a single-line __asm, we're done. |
| 1668 | if (!InBraces) |
| 1669 | break; |
| 1670 | // We're no longer in a comment. |
| 1671 | InAsmComment = false; |
| 1672 | } else if (!InAsmComment && Tok.is(tok::r_brace)) { |
| 1673 | // Single-line asm always ends when a closing brace is seen. |
| 1674 | // FIXME: This is compatible with Apple gcc's -fasm-blocks; what |
| 1675 | // does MSVC do here? |
| 1676 | break; |
| 1677 | } |
| 1678 | } |
| 1679 | if (!InAsmComment && InBraces && Tok.is(tok::r_brace) && |
| 1680 | BraceCount == (savedBraceCount + 1)) { |
| 1681 | // Consume the closing brace, and finish |
| 1682 | EndLoc = ConsumeBrace(); |
| 1683 | break; |
| 1684 | } |
| 1685 | |
| 1686 | // Consume the next token; make sure we don't modify the brace count etc. |
| 1687 | // if we are in a comment. |
| 1688 | EndLoc = TokLoc; |
| 1689 | if (InAsmComment) |
| 1690 | PP.Lex(Tok); |
| 1691 | else { |
| 1692 | AsmToks.push_back(Tok); |
| 1693 | ConsumeAnyToken(); |
| 1694 | } |
| 1695 | TokLoc = Tok.getLocation(); |
| 1696 | ++NumTokensRead; |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1697 | } while (1); |
Chad Rosier | 8cd64b4 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 1698 | |
Chad Rosier | 21ef711 | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 1699 | if (InBraces && BraceCount != savedBraceCount) { |
| 1700 | // __asm without closing brace (this can happen at EOF). |
| 1701 | Diag(Tok, diag::err_expected_rbrace); |
| 1702 | Diag(LBraceLoc, diag::note_matching) << "{"; |
| 1703 | return StmtError(); |
| 1704 | } else if (NumTokensRead == 0) { |
| 1705 | // Empty __asm. |
| 1706 | Diag(Tok, diag::err_expected_lbrace); |
| 1707 | return StmtError(); |
| 1708 | } |
| 1709 | |
Chad Rosier | 56d7f23 | 2012-08-24 21:42:51 +0000 | [diff] [blame] | 1710 | // If MS-style inline assembly is disabled, then build an empty asm. |
| 1711 | if (!getLangOpts().EmitMicrosoftInlineAsm) { |
| 1712 | Token t; |
| 1713 | t.setKind(tok::string_literal); |
| 1714 | t.setLiteralData("\"/*FIXME: not done*/\""); |
| 1715 | t.clearFlag(Token::NeedsCleaning); |
| 1716 | t.setLength(21); |
| 1717 | ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1)); |
| 1718 | ExprVector Constraints; |
| 1719 | ExprVector Exprs; |
| 1720 | ExprVector Clobbers; |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 1721 | return Actions.ActOnGCCAsmStmt(AsmLoc, true, true, 0, 0, 0, Constraints, |
| 1722 | Exprs, AsmString.take(), Clobbers, EndLoc); |
Chad Rosier | 56d7f23 | 2012-08-24 21:42:51 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Chad Rosier | 8f726de | 2012-08-06 20:03:45 +0000 | [diff] [blame] | 1725 | // FIXME: We should be passing source locations for better diagnostics. |
Chad Rosier | 7bd092b | 2012-08-15 16:53:30 +0000 | [diff] [blame] | 1726 | return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc, |
| 1727 | llvm::makeArrayRef(AsmToks), EndLoc); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1731 | /// asm-statement: |
| 1732 | /// gnu-asm-statement |
| 1733 | /// ms-asm-statement |
| 1734 | /// |
| 1735 | /// [GNU] gnu-asm-statement: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1736 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 1737 | /// |
| 1738 | /// [GNU] asm-argument: |
| 1739 | /// asm-string-literal |
| 1740 | /// asm-string-literal ':' asm-operands[opt] |
| 1741 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 1742 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 1743 | /// ':' asm-clobbers |
| 1744 | /// |
| 1745 | /// [GNU] asm-clobbers: |
| 1746 | /// asm-string-literal |
| 1747 | /// asm-clobbers ',' asm-string-literal |
| 1748 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1749 | StmtResult Parser::ParseAsmStatement(bool &msAsm) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1750 | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 1751 | SourceLocation AsmLoc = ConsumeToken(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1752 | |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1753 | if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) && |
| 1754 | !isTypeQualifier()) { |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1755 | msAsm = true; |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1756 | return ParseMicrosoftAsmStatement(AsmLoc); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1757 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1758 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1759 | SourceLocation Loc = Tok.getLocation(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1760 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1761 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1762 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
| 1763 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1764 | Diag(Loc, diag::w_asm_qualifier_ignored) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1765 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1766 | Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict"; |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1767 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1768 | // Remember if this was a volatile asm. |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 1769 | bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1770 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1771 | Diag(Tok, diag::err_expected_lparen_after) << "asm"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | SkipUntil(tok::r_paren); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1773 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1774 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1775 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1776 | T.consumeOpen(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1777 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1778 | ExprResult AsmString(ParseAsmStringLiteral()); |
Ted Kremenek | 320fa4b | 2011-12-02 01:30:14 +0000 | [diff] [blame] | 1779 | if (AsmString.isInvalid()) { |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 1780 | // Consume up to and including the closing paren. |
| 1781 | T.skipToEnd(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1782 | return StmtError(); |
Ted Kremenek | 320fa4b | 2011-12-02 01:30:14 +0000 | [diff] [blame] | 1783 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1785 | SmallVector<IdentifierInfo *, 4> Names; |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1786 | ExprVector Constraints; |
| 1787 | ExprVector Exprs; |
| 1788 | ExprVector Clobbers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1789 | |
Anders Carlsson | dfab34a | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 1790 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1791 | // We have a simple asm expression like 'asm("foo")'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1792 | T.consumeClose(); |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 1793 | return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile, |
| 1794 | /*NumOutputs*/ 0, /*NumInputs*/ 0, 0, |
| 1795 | Constraints, Exprs, AsmString.take(), |
| 1796 | Clobbers, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1797 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1799 | // Parse Outputs, if present. |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1800 | bool AteExtraColon = false; |
| 1801 | if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 1802 | // In C++ mode, parse "::" like ": :". |
| 1803 | AteExtraColon = Tok.is(tok::coloncolon); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1804 | ConsumeToken(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1805 | |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1806 | if (!AteExtraColon && |
| 1807 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1808 | return StmtError(); |
| 1809 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1810 | |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1811 | unsigned NumOutputs = Names.size(); |
| 1812 | |
| 1813 | // Parse Inputs, if present. |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1814 | if (AteExtraColon || |
| 1815 | Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 1816 | // In C++ mode, parse "::" like ": :". |
| 1817 | if (AteExtraColon) |
| 1818 | AteExtraColon = false; |
| 1819 | else { |
| 1820 | AteExtraColon = Tok.is(tok::coloncolon); |
| 1821 | ConsumeToken(); |
| 1822 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1823 | |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1824 | if (!AteExtraColon && |
| 1825 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1826 | return StmtError(); |
| 1827 | } |
| 1828 | |
| 1829 | assert(Names.size() == Constraints.size() && |
| 1830 | Constraints.size() == Exprs.size() && |
| 1831 | "Input operand size mismatch!"); |
| 1832 | |
| 1833 | unsigned NumInputs = Names.size() - NumOutputs; |
| 1834 | |
| 1835 | // Parse the clobbers, if present. |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1836 | if (AteExtraColon || Tok.is(tok::colon)) { |
| 1837 | if (!AteExtraColon) |
| 1838 | ConsumeToken(); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1839 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1840 | // Parse the asm-string list for clobbers if present. |
| 1841 | if (Tok.isNot(tok::r_paren)) { |
| 1842 | while (1) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1843 | ExprResult Clobber(ParseAsmStringLiteral()); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1844 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1845 | if (Clobber.isInvalid()) |
| 1846 | break; |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1847 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1848 | Clobbers.push_back(Clobber.release()); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1849 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1850 | if (Tok.isNot(tok::comma)) break; |
| 1851 | ConsumeToken(); |
| 1852 | } |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1856 | T.consumeClose(); |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 1857 | return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs, |
| 1858 | NumInputs, Names.data(), Constraints, Exprs, |
| 1859 | AsmString.take(), Clobbers, |
| 1860 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | /// ParseAsmOperands - Parse the asm-operands production as used by |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1864 | /// asm-statement, assuming the leading ':' token was eaten. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1865 | /// |
| 1866 | /// [GNU] asm-operands: |
| 1867 | /// asm-operand |
| 1868 | /// asm-operands ',' asm-operand |
| 1869 | /// |
| 1870 | /// [GNU] asm-operand: |
| 1871 | /// asm-string-literal '(' expression ')' |
| 1872 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 1873 | /// |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 1874 | // |
| 1875 | // FIXME: Avoid unnecessary std::string trashing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1876 | bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1877 | SmallVectorImpl<Expr *> &Constraints, |
| 1878 | SmallVectorImpl<Expr *> &Exprs) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1879 | // 'asm-operands' isn't present? |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1880 | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1881 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | |
| 1883 | while (1) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1884 | // Read the [id] if present. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1885 | if (Tok.is(tok::l_square)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1886 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 1887 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1889 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1890 | Diag(Tok, diag::err_expected_ident); |
| 1891 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1892 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1895 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 69efba7 | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 1896 | ConsumeToken(); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1898 | Names.push_back(II); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1899 | T.consumeClose(); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1900 | } else |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1901 | Names.push_back(0); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1902 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1903 | ExprResult Constraint(ParseAsmStringLiteral()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1904 | if (Constraint.isInvalid()) { |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1905 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1906 | return true; |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1907 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1908 | Constraints.push_back(Constraint.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1909 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1910 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1911 | Diag(Tok, diag::err_expected_lparen_after) << "asm operand"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1912 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1913 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1914 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1915 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1916 | // Read the parenthesized expression. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1917 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1918 | T.consumeOpen(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1919 | ExprResult Res(ParseExpression()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1920 | T.consumeClose(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1921 | if (Res.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1922 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1923 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1924 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1925 | Exprs.push_back(Res.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1926 | // Eat the comma and continue parsing if it exists. |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1927 | if (Tok.isNot(tok::comma)) return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1928 | ConsumeToken(); |
| 1929 | } |
| 1930 | } |
Fariborz Jahanian | f9ed315 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1932 | Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) { |
Chris Lattner | 40e9bc8 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 1933 | assert(Tok.is(tok::l_brace)); |
| 1934 | SourceLocation LBraceLoc = Tok.getLocation(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1935 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1936 | if (SkipFunctionBodies && trySkippingFunctionBody()) { |
| 1937 | BodyScope.Exit(); |
| 1938 | return Actions.ActOnFinishFunctionBody(Decl, 0); |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1939 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1940 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1941 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc, |
| 1942 | "parsing function body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Fariborz Jahanian | f9ed315 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1944 | // Do not enter a scope for the brace, as the arguments are in the same scope |
| 1945 | // (the function body) as the body itself. Instead, just read the statement |
| 1946 | // list and put it into a CompoundStmt for safe keeping. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1947 | StmtResult FnBody(ParseCompoundStatementBody()); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1948 | |
Fariborz Jahanian | f9ed315 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1949 | // If the function body could not be parsed, make a bogus compoundstmt. |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1950 | if (FnBody.isInvalid()) { |
| 1951 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1953 | MultiStmtArg(), false); |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1954 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1956 | BodyScope.Exit(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1957 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Seo Sanghyeon | cd5af4b | 2007-12-01 08:06:07 +0000 | [diff] [blame] | 1958 | } |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1959 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1960 | /// ParseFunctionTryBlock - Parse a C++ function-try-block. |
| 1961 | /// |
| 1962 | /// function-try-block: |
| 1963 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 1964 | /// |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1965 | Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) { |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1966 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 1967 | SourceLocation TryLoc = ConsumeToken(); |
| 1968 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1969 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc, |
| 1970 | "parsing function try block"); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1971 | |
| 1972 | // Constructor initializer list? |
| 1973 | if (Tok.is(tok::colon)) |
| 1974 | ParseConstructorInitializer(Decl); |
Douglas Gregor | 2eef427 | 2011-09-07 20:36:12 +0000 | [diff] [blame] | 1975 | else |
| 1976 | Actions.ActOnDefaultCtorInitializers(Decl); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1977 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1978 | if (SkipFunctionBodies && trySkippingFunctionBody()) { |
| 1979 | BodyScope.Exit(); |
| 1980 | return Actions.ActOnFinishFunctionBody(Decl, 0); |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1981 | } |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 1982 | |
Sebastian Redl | de1b60a | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 1983 | SourceLocation LBraceLoc = Tok.getLocation(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1984 | StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc)); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1985 | // If we failed to parse the try-catch, we just give the function an empty |
| 1986 | // compound statement as the body. |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1987 | if (FnBody.isInvalid()) { |
| 1988 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Sebastian Redl | de1b60a | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 1989 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1990 | MultiStmtArg(), false); |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1991 | } |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1993 | BodyScope.Exit(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1994 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1997 | bool Parser::trySkippingFunctionBody() { |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 1998 | assert(Tok.is(tok::l_brace)); |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1999 | assert(SkipFunctionBodies && |
| 2000 | "Should only be called when SkipFunctionBodies is enabled"); |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2001 | |
| 2002 | // We're in code-completion mode. Skip parsing for all function bodies unless |
| 2003 | // the body contains the code-completion point. |
| 2004 | TentativeParsingAction PA(*this); |
| 2005 | ConsumeBrace(); |
| 2006 | if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false, |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2007 | /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) { |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2008 | PA.Commit(); |
| 2009 | return true; |
| 2010 | } |
| 2011 | |
| 2012 | PA.Revert(); |
| 2013 | return false; |
| 2014 | } |
| 2015 | |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2016 | /// ParseCXXTryBlock - Parse a C++ try-block. |
| 2017 | /// |
| 2018 | /// try-block: |
| 2019 | /// 'try' compound-statement handler-seq |
| 2020 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2021 | StmtResult Parser::ParseCXXTryBlock() { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2022 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 2023 | |
| 2024 | SourceLocation TryLoc = ConsumeToken(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2025 | return ParseCXXTryBlockCommon(TryLoc); |
| 2026 | } |
| 2027 | |
| 2028 | /// ParseCXXTryBlockCommon - Parse the common part of try-block and |
| 2029 | /// function-try-block. |
| 2030 | /// |
| 2031 | /// try-block: |
| 2032 | /// 'try' compound-statement handler-seq |
| 2033 | /// |
| 2034 | /// function-try-block: |
| 2035 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 2036 | /// |
| 2037 | /// handler-seq: |
| 2038 | /// handler handler-seq[opt] |
| 2039 | /// |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2040 | /// [Borland] try-block: |
| 2041 | /// 'try' compound-statement seh-except-block |
| 2042 | /// 'try' compound-statment seh-finally-block |
| 2043 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2044 | StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2045 | if (Tok.isNot(tok::l_brace)) |
| 2046 | return StmtError(Diag(Tok, diag::err_expected_lbrace)); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2047 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2048 | |
| 2049 | StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2050 | Scope::DeclScope|Scope::TryScope)); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2051 | if (TryBlock.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2052 | return TryBlock; |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2053 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2054 | // Borland allows SEH-handlers with 'try' |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2055 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2056 | if ((Tok.is(tok::identifier) && |
| 2057 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) || |
| 2058 | Tok.is(tok::kw___finally)) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2059 | // TODO: Factor into common return ParseSEHHandlerCommon(...) |
| 2060 | StmtResult Handler; |
Douglas Gregor | b57791e | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 2061 | if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2062 | SourceLocation Loc = ConsumeToken(); |
| 2063 | Handler = ParseSEHExceptBlock(Loc); |
| 2064 | } |
| 2065 | else { |
| 2066 | SourceLocation Loc = ConsumeToken(); |
| 2067 | Handler = ParseSEHFinallyBlock(Loc); |
| 2068 | } |
| 2069 | if(Handler.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2070 | return Handler; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2071 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2072 | return Actions.ActOnSEHTryBlock(true /* IsCXXTry */, |
| 2073 | TryLoc, |
| 2074 | TryBlock.take(), |
| 2075 | Handler.take()); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2076 | } |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2077 | else { |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2078 | StmtVector Handlers; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2079 | ParsedAttributesWithRange attrs(AttrFactory); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2080 | MaybeParseCXX0XAttributes(attrs); |
| 2081 | ProhibitAttributes(attrs); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2082 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2083 | if (Tok.isNot(tok::kw_catch)) |
| 2084 | return StmtError(Diag(Tok, diag::err_expected_catch)); |
| 2085 | while (Tok.is(tok::kw_catch)) { |
| 2086 | StmtResult Handler(ParseCXXCatchBlock()); |
| 2087 | if (!Handler.isInvalid()) |
| 2088 | Handlers.push_back(Handler.release()); |
| 2089 | } |
| 2090 | // Don't bother creating the full statement if we don't have any usable |
| 2091 | // handlers. |
| 2092 | if (Handlers.empty()) |
| 2093 | return StmtError(); |
| 2094 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2095 | return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2096 | } |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard |
| 2100 | /// |
| 2101 | /// handler: |
| 2102 | /// 'catch' '(' exception-declaration ')' compound-statement |
| 2103 | /// |
| 2104 | /// exception-declaration: |
| 2105 | /// type-specifier-seq declarator |
| 2106 | /// type-specifier-seq abstract-declarator |
| 2107 | /// type-specifier-seq |
| 2108 | /// '...' |
| 2109 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2110 | StmtResult Parser::ParseCXXCatchBlock() { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2111 | assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); |
| 2112 | |
| 2113 | SourceLocation CatchLoc = ConsumeToken(); |
| 2114 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2115 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2116 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2117 | return StmtError(); |
| 2118 | |
| 2119 | // C++ 3.3.2p3: |
| 2120 | // The name in a catch exception-declaration is local to the handler and |
| 2121 | // shall not be redeclared in the outermost block of the handler. |
| 2122 | ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope); |
| 2123 | |
| 2124 | // exception-declaration is equivalent to '...' or a parameter-declaration |
| 2125 | // without default arguments. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2126 | Decl *ExceptionDecl = 0; |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2127 | if (Tok.isNot(tok::ellipsis)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2128 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2129 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 2130 | return StmtError(); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2131 | Declarator ExDecl(DS, Declarator::CXXCatchContext); |
| 2132 | ParseDeclarator(ExDecl); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2133 | ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2134 | } else |
| 2135 | ConsumeToken(); |
| 2136 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2137 | T.consumeClose(); |
| 2138 | if (T.getCloseLocation().isInvalid()) |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2139 | return StmtError(); |
| 2140 | |
| 2141 | if (Tok.isNot(tok::l_brace)) |
| 2142 | return StmtError(Diag(Tok, diag::err_expected_lbrace)); |
| 2143 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2144 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2145 | StmtResult Block(ParseCompoundStatement()); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2146 | if (Block.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2147 | return Block; |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2148 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2149 | return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take()); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2150 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2151 | |
| 2152 | void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) { |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2153 | IfExistsCondition Result; |
Francois Pichet | f986038 | 2011-05-07 17:30:27 +0000 | [diff] [blame] | 2154 | if (ParseMicrosoftIfExistsCondition(Result)) |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2155 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2156 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2157 | // Handle dependent statements by parsing the braces as a compound statement. |
| 2158 | // This is not the same behavior as Visual C++, which don't treat this as a |
| 2159 | // compound statement, but for Clang's type checking we can't have anything |
| 2160 | // inside these braces escaping to the surrounding code. |
| 2161 | if (Result.Behavior == IEB_Dependent) { |
| 2162 | if (!Tok.is(tok::l_brace)) { |
| 2163 | Diag(Tok, diag::err_expected_lbrace); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2164 | return; |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2165 | } |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2166 | |
| 2167 | StmtResult Compound = ParseCompoundStatement(); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2168 | if (Compound.isInvalid()) |
| 2169 | return; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2171 | StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc, |
| 2172 | Result.IsIfExists, |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2173 | Result.SS, |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2174 | Result.Name, |
| 2175 | Compound.get()); |
| 2176 | if (DepResult.isUsable()) |
| 2177 | Stmts.push_back(DepResult.get()); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2178 | return; |
| 2179 | } |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2180 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2181 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 2182 | if (Braces.consumeOpen()) { |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2183 | Diag(Tok, diag::err_expected_lbrace); |
| 2184 | return; |
| 2185 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2186 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2187 | switch (Result.Behavior) { |
| 2188 | case IEB_Parse: |
| 2189 | // Parse the statements below. |
| 2190 | break; |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2192 | case IEB_Dependent: |
| 2193 | llvm_unreachable("Dependent case handled above"); |
Chad Rosier | b660446 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2195 | case IEB_Skip: |
| 2196 | Braces.skipToEnd(); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2197 | return; |
| 2198 | } |
| 2199 | |
| 2200 | // Condition is true, parse the statements. |
| 2201 | while (Tok.isNot(tok::r_brace)) { |
| 2202 | StmtResult R = ParseStatementOrDeclaration(Stmts, false); |
| 2203 | if (R.isUsable()) |
| 2204 | Stmts.push_back(R.release()); |
| 2205 | } |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2206 | Braces.consumeClose(); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2207 | } |