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