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