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