Chris Lattner | 7ad0fbe | 2006-11-05 07:46:30 +0000 | [diff] [blame] | 1 | //===--- ParseStmt.cpp - Statement and Block Parser -----------------------===// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Statement and Block portions of the Parser |
| 11 | // interface. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 8a9a97a | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 16 | #include "RAIIObjectsForParser.h" |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
| 19 | #include "clang/Basic/PrettyStackTrace.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Scope.h" |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/TypoCorrection.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCAsmInfo.h" |
| 28 | #include "llvm/MC/MCContext.h" |
| 29 | #include "llvm/MC/MCObjectFileInfo.h" |
| 30 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 31 | #include "llvm/MC/MCRegisterInfo.h" |
| 32 | #include "llvm/MC/MCStreamer.h" |
| 33 | #include "llvm/MC/MCSubtargetInfo.h" |
| 34 | #include "llvm/MC/MCTargetAsmParser.h" |
| 35 | #include "llvm/Support/SourceMgr.h" |
| 36 | #include "llvm/Support/TargetRegistry.h" |
| 37 | #include "llvm/Support/TargetSelect.h" |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 38 | using namespace clang; |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // C99 6.8: Statements and Blocks. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Richard Smith | 426a47b | 2013-10-28 22:04:30 +0000 | [diff] [blame] | 44 | /// \brief Parse a standalone statement (for instance, as the body of an 'if', |
| 45 | /// 'while', or 'for'). |
| 46 | StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc) { |
| 47 | StmtResult Res; |
| 48 | |
| 49 | // We may get back a null statement if we found a #pragma. Keep going until |
| 50 | // we get an actual statement. |
| 51 | do { |
| 52 | StmtVector Stmts; |
| 53 | Res = ParseStatementOrDeclaration(Stmts, true, TrailingElseLoc); |
| 54 | } while (!Res.isInvalid() && !Res.get()); |
| 55 | |
| 56 | return Res; |
| 57 | } |
| 58 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 59 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 60 | /// StatementOrDeclaration: |
| 61 | /// statement |
| 62 | /// declaration |
| 63 | /// |
| 64 | /// statement: |
| 65 | /// labeled-statement |
| 66 | /// compound-statement |
| 67 | /// expression-statement |
| 68 | /// selection-statement |
| 69 | /// iteration-statement |
| 70 | /// jump-statement |
Argyrios Kyrtzidis | dee8291 | 2008-09-07 18:58:01 +0000 | [diff] [blame] | 71 | /// [C++] declaration-statement |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 72 | /// [C++] try-block |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 73 | /// [MS] seh-try-block |
Fariborz Jahanian | 9081457 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 74 | /// [OBC] objc-throw-statement |
| 75 | /// [OBC] objc-try-catch-statement |
Fariborz Jahanian | f89ca38 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 76 | /// [OBC] objc-synchronized-statement |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 77 | /// [GNU] asm-statement |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 78 | /// [OMP] openmp-construct [TODO] |
| 79 | /// |
| 80 | /// labeled-statement: |
| 81 | /// identifier ':' statement |
| 82 | /// 'case' constant-expression ':' statement |
| 83 | /// 'default' ':' statement |
| 84 | /// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 85 | /// selection-statement: |
| 86 | /// if-statement |
| 87 | /// switch-statement |
| 88 | /// |
| 89 | /// iteration-statement: |
| 90 | /// while-statement |
| 91 | /// do-statement |
| 92 | /// for-statement |
| 93 | /// |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 94 | /// expression-statement: |
| 95 | /// expression[opt] ';' |
| 96 | /// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 97 | /// jump-statement: |
| 98 | /// 'goto' identifier ';' |
| 99 | /// 'continue' ';' |
| 100 | /// 'break' ';' |
| 101 | /// 'return' expression[opt] ';' |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 102 | /// [GNU] 'goto' '*' expression ';' |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 103 | /// |
Fariborz Jahanian | 9081457 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 104 | /// [OBC] objc-throw-statement: |
| 105 | /// [OBC] '@' 'throw' expression ';' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | /// [OBC] '@' 'throw' ';' |
| 107 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 108 | StmtResult |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 109 | Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement, |
| 110 | SourceLocation *TrailingElseLoc) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 111 | |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 112 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 113 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 114 | ParsedAttributesWithRange Attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 115 | MaybeParseCXX11Attributes(Attrs, 0, /*MightBeObjCMessageSend*/ true); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 116 | |
| 117 | StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts, |
| 118 | OnlyStatement, TrailingElseLoc, Attrs); |
| 119 | |
| 120 | assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) && |
| 121 | "attributes on empty statement"); |
| 122 | |
| 123 | if (Attrs.empty() || Res.isInvalid()) |
| 124 | return Res; |
| 125 | |
| 126 | return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range); |
| 127 | } |
| 128 | |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 129 | namespace { |
| 130 | class StatementFilterCCC : public CorrectionCandidateCallback { |
| 131 | public: |
| 132 | StatementFilterCCC(Token nextTok) : NextToken(nextTok) { |
| 133 | WantTypeSpecifiers = nextTok.is(tok::l_paren) || nextTok.is(tok::less) || |
| 134 | nextTok.is(tok::identifier) || nextTok.is(tok::star) || |
| 135 | nextTok.is(tok::amp) || nextTok.is(tok::l_square); |
| 136 | WantExpressionKeywords = nextTok.is(tok::l_paren) || |
| 137 | nextTok.is(tok::identifier) || |
| 138 | nextTok.is(tok::arrow) || nextTok.is(tok::period); |
| 139 | WantRemainingKeywords = nextTok.is(tok::l_paren) || nextTok.is(tok::semi) || |
| 140 | nextTok.is(tok::identifier) || |
| 141 | nextTok.is(tok::l_brace); |
| 142 | WantCXXNamedCasts = false; |
| 143 | } |
| 144 | |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 145 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 146 | if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>()) |
Kaelyn Uhrain | 07e6272 | 2013-10-01 22:00:28 +0000 | [diff] [blame] | 147 | return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD); |
Kaelyn Uhrain | 46b6cdc | 2013-09-27 19:40:16 +0000 | [diff] [blame] | 148 | if (NextToken.is(tok::equal)) |
| 149 | return candidate.getCorrectionDeclAs<VarDecl>(); |
Kaelyn Uhrain | 30943ce | 2013-09-27 23:54:23 +0000 | [diff] [blame] | 150 | if (NextToken.is(tok::period) && |
| 151 | candidate.getCorrectionDeclAs<NamespaceDecl>()) |
| 152 | return false; |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 153 | return CorrectionCandidateCallback::ValidateCandidate(candidate); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | Token NextToken; |
| 158 | }; |
| 159 | } |
| 160 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 161 | StmtResult |
| 162 | Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts, |
| 163 | bool OnlyStatement, SourceLocation *TrailingElseLoc, |
| 164 | ParsedAttributesWithRange &Attrs) { |
| 165 | const char *SemiError = 0; |
| 166 | StmtResult Res; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 168 | // Cases in this switch statement should fall through if the parser expects |
| 169 | // the token to end in a semicolon (in which case SemiError should be set), |
| 170 | // or they directly 'return;' if not. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 171 | Retry: |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 172 | tok::TokenKind Kind = Tok.getKind(); |
| 173 | SourceLocation AtLoc; |
| 174 | switch (Kind) { |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 175 | case tok::at: // May be a @try or @throw statement |
| 176 | { |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 177 | ProhibitAttributes(Attrs); // TODO: is it correct? |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 178 | AtLoc = ConsumeToken(); // consume @ |
Sebastian Redl | bab9a4b | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 179 | return ParseObjCAtStatement(AtLoc); |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 180 | } |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 181 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 182 | case tok::code_completion: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 183 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 184 | cutOffParsing(); |
| 185 | return StmtError(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 187 | case tok::identifier: { |
| 188 | Token Next = NextToken(); |
| 189 | if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement |
Argyrios Kyrtzidis | 07b8b63 | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 190 | // identifier ':' statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 191 | return ParseLabeledStatement(Attrs); |
Argyrios Kyrtzidis | 07b8b63 | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 192 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 193 | |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 194 | // Look up the identifier, and typo-correct it to a keyword if it's not |
| 195 | // found. |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 196 | if (Next.isNot(tok::coloncolon)) { |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 197 | // Try to limit which sets of keywords should be included in typo |
| 198 | // correction based on what the next token is. |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 199 | StatementFilterCCC Validator(Next); |
| 200 | if (TryAnnotateName(/*IsAddressOfOperand*/false, &Validator) |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 201 | == ANK_Error) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 202 | // Handle errors here by skipping up to the next semicolon or '}', and |
| 203 | // eat the semicolon if that's what stopped us. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 204 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 205 | if (Tok.is(tok::semi)) |
| 206 | ConsumeToken(); |
| 207 | return StmtError(); |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 208 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 209 | |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 210 | // If the identifier was typo-corrected, try again. |
| 211 | if (Tok.isNot(tok::identifier)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 212 | goto Retry; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 213 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 215 | // Fall through |
| 216 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 218 | default: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 219 | if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) { |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 220 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 221 | DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 222 | DeclEnd, Attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 223 | return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 227 | Diag(Tok, diag::err_expected_statement); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 228 | return StmtError(); |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 229 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 231 | return ParseExprStatement(); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 232 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 233 | |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 234 | case tok::kw_case: // C99 6.8.1: labeled-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 235 | return ParseCaseStatement(); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 236 | case tok::kw_default: // C99 6.8.1: labeled-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 237 | return ParseDefaultStatement(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 239 | case tok::l_brace: // C99 6.8.2: compound-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 240 | return ParseCompoundStatement(); |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 241 | case tok::semi: { // C99 6.8.3p3: expression[opt] ';' |
Argyrios Kyrtzidis | 43ea78b | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 242 | bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro(); |
| 243 | return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro); |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 244 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 246 | case tok::kw_if: // C99 6.8.4.1: if-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 247 | return ParseIfStatement(TrailingElseLoc); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 248 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 249 | return ParseSwitchStatement(TrailingElseLoc); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 251 | case tok::kw_while: // C99 6.8.5.1: while-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 252 | return ParseWhileStatement(TrailingElseLoc); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 253 | case tok::kw_do: // C99 6.8.5.2: do-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 254 | Res = ParseDoStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 255 | SemiError = "do/while"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 256 | break; |
| 257 | case tok::kw_for: // C99 6.8.5.3: for-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 258 | return ParseForStatement(TrailingElseLoc); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 259 | |
| 260 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 261 | Res = ParseGotoStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 262 | SemiError = "goto"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 263 | break; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 264 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 265 | Res = ParseContinueStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 266 | SemiError = "continue"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 267 | break; |
| 268 | case tok::kw_break: // C99 6.8.6.3: break-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 269 | Res = ParseBreakStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 270 | SemiError = "break"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 271 | break; |
| 272 | case tok::kw_return: // C99 6.8.6.4: return-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 273 | Res = ParseReturnStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 274 | SemiError = "return"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 275 | break; |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 276 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 277 | case tok::kw_asm: { |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 278 | ProhibitAttributes(Attrs); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 279 | bool msAsm = false; |
| 280 | Res = ParseAsmStatement(msAsm); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 281 | Res = Actions.ActOnFinishFullStmt(Res.get()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 282 | if (msAsm) return Res; |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 283 | SemiError = "asm"; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 284 | break; |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 285 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 286 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 287 | case tok::kw_try: // C++ 15: try-block |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 288 | return ParseCXXTryBlock(); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 289 | |
| 290 | case tok::kw___try: |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 291 | ProhibitAttributes(Attrs); // TODO: is it correct? |
| 292 | return ParseSEHTryBlock(); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 293 | |
| 294 | case tok::annot_pragma_vis: |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 295 | ProhibitAttributes(Attrs); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 296 | HandlePragmaVisibility(); |
| 297 | return StmtEmpty(); |
| 298 | |
| 299 | case tok::annot_pragma_pack: |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 300 | ProhibitAttributes(Attrs); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 301 | HandlePragmaPack(); |
| 302 | return StmtEmpty(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 303 | |
Eli Friedman | bbbbac6 | 2012-10-09 22:46:54 +0000 | [diff] [blame] | 304 | case tok::annot_pragma_msstruct: |
| 305 | ProhibitAttributes(Attrs); |
| 306 | HandlePragmaMSStruct(); |
| 307 | return StmtEmpty(); |
| 308 | |
Eli Friedman | ae8ee25 | 2012-10-08 23:52:38 +0000 | [diff] [blame] | 309 | case tok::annot_pragma_align: |
| 310 | ProhibitAttributes(Attrs); |
| 311 | HandlePragmaAlign(); |
| 312 | return StmtEmpty(); |
| 313 | |
Eli Friedman | bbbbac6 | 2012-10-09 22:46:54 +0000 | [diff] [blame] | 314 | case tok::annot_pragma_weak: |
| 315 | ProhibitAttributes(Attrs); |
| 316 | HandlePragmaWeak(); |
| 317 | return StmtEmpty(); |
| 318 | |
| 319 | case tok::annot_pragma_weakalias: |
| 320 | ProhibitAttributes(Attrs); |
| 321 | HandlePragmaWeakAlias(); |
| 322 | return StmtEmpty(); |
| 323 | |
| 324 | case tok::annot_pragma_redefine_extname: |
| 325 | ProhibitAttributes(Attrs); |
| 326 | HandlePragmaRedefineExtname(); |
| 327 | return StmtEmpty(); |
| 328 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 329 | case tok::annot_pragma_fp_contract: |
Richard Smith | ca9b0b6 | 2013-11-15 21:10:54 +0000 | [diff] [blame] | 330 | ProhibitAttributes(Attrs); |
Lang Hames | a930e71 | 2012-10-21 01:10:01 +0000 | [diff] [blame] | 331 | Diag(Tok, diag::err_pragma_fp_contract_scope); |
| 332 | ConsumeToken(); |
| 333 | return StmtError(); |
| 334 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 335 | case tok::annot_pragma_opencl_extension: |
| 336 | ProhibitAttributes(Attrs); |
| 337 | HandlePragmaOpenCLExtension(); |
| 338 | return StmtEmpty(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 339 | |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 340 | case tok::annot_pragma_captured: |
Richard Smith | 12a41bd | 2013-09-16 21:17:44 +0000 | [diff] [blame] | 341 | ProhibitAttributes(Attrs); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 342 | return HandlePragmaCaptured(); |
| 343 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 344 | case tok::annot_pragma_openmp: |
Richard Smith | 12a41bd | 2013-09-16 21:17:44 +0000 | [diff] [blame] | 345 | ProhibitAttributes(Attrs); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 346 | return ParseOpenMPDeclarativeOrExecutableDirective(); |
| 347 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 348 | case tok::annot_pragma_ms_pointers_to_members: |
| 349 | ProhibitAttributes(Attrs); |
| 350 | HandlePragmaMSPointersToMembers(); |
| 351 | return StmtEmpty(); |
| 352 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 353 | case tok::annot_pragma_ms_pragma: |
| 354 | ProhibitAttributes(Attrs); |
| 355 | HandlePragmaMSPragma(); |
| 356 | return StmtEmpty(); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 359 | // If we reached this code, the statement must end in a semicolon. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 360 | if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) { |
Chris Lattner | 8e3eed0 | 2009-06-14 00:23:56 +0000 | [diff] [blame] | 361 | // If the result was valid, then we do want to diagnose this. Use |
| 362 | // ExpectAndConsume to emit the diagnostic, even though we know it won't |
| 363 | // succeed. |
| 364 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError); |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 365 | // Skip until we see a } or ;, but don't eat it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 366 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 367 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 369 | return Res; |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 372 | /// \brief Parse an expression statement. |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 373 | StmtResult Parser::ParseExprStatement() { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 374 | // If a case keyword is missing, this is where it should be inserted. |
| 375 | Token OldToken = Tok; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 377 | // expression[opt] ';' |
Douglas Gregor | da6c89d | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 378 | ExprResult Expr(ParseExpression()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 379 | if (Expr.isInvalid()) { |
| 380 | // If the expression is invalid, skip ahead to the next semicolon or '}'. |
| 381 | // Not doing this opens us up to the possibility of infinite loops if |
| 382 | // ParseExpression does not consume any tokens. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 383 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 384 | if (Tok.is(tok::semi)) |
| 385 | ConsumeToken(); |
John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 386 | return Actions.ActOnExprStmtError(); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 387 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 389 | if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() && |
| 390 | Actions.CheckCaseExpression(Expr.get())) { |
| 391 | // If a constant expression is followed by a colon inside a switch block, |
| 392 | // suggest a missing case keyword. |
| 393 | Diag(OldToken, diag::err_expected_case_before_expression) |
| 394 | << FixItHint::CreateInsertion(OldToken.getLocation(), "case "); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 396 | // Recover parsing as a case statement. |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 397 | return ParseCaseStatement(/*MissingCase=*/true, Expr); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 398 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 400 | // Otherwise, eat the semicolon. |
| 401 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 402 | return Actions.ActOnExprStmt(Expr); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 403 | } |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 404 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 405 | StmtResult Parser::ParseSEHTryBlock() { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 406 | assert(Tok.is(tok::kw___try) && "Expected '__try'"); |
| 407 | SourceLocation Loc = ConsumeToken(); |
| 408 | return ParseSEHTryBlockCommon(Loc); |
| 409 | } |
| 410 | |
| 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 | /// |
| 420 | StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) { |
| 421 | if(Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 422 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 423 | |
Joao Matos | 566359c | 2012-09-04 17:49:35 +0000 | [diff] [blame] | 424 | StmtResult TryBlock(ParseCompoundStatement()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 425 | if(TryBlock.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 426 | return TryBlock; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 427 | |
| 428 | StmtResult Handler; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 429 | if (Tok.is(tok::identifier) && |
Douglas Gregor | 60060d6 | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 430 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 431 | SourceLocation Loc = ConsumeToken(); |
| 432 | Handler = ParseSEHExceptBlock(Loc); |
| 433 | } else if (Tok.is(tok::kw___finally)) { |
| 434 | SourceLocation Loc = ConsumeToken(); |
| 435 | Handler = ParseSEHFinallyBlock(Loc); |
| 436 | } else { |
| 437 | return StmtError(Diag(Tok,diag::err_seh_expected_handler)); |
| 438 | } |
| 439 | |
| 440 | if(Handler.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 441 | return Handler; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 442 | |
| 443 | return Actions.ActOnSEHTryBlock(false /* IsCXXTry */, |
| 444 | TryLoc, |
| 445 | TryBlock.take(), |
| 446 | Handler.take()); |
| 447 | } |
| 448 | |
| 449 | /// ParseSEHExceptBlock - Handle __except |
| 450 | /// |
| 451 | /// seh-except-block: |
| 452 | /// '__except' '(' seh-filter-expression ')' compound-statement |
| 453 | /// |
| 454 | StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) { |
| 455 | PoisonIdentifierRAIIObject raii(Ident__exception_code, false), |
| 456 | raii2(Ident___exception_code, false), |
| 457 | raii3(Ident_GetExceptionCode, false); |
| 458 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 459 | if (ExpectAndConsume(tok::l_paren)) |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 460 | return StmtError(); |
| 461 | |
| 462 | ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope); |
| 463 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 464 | if (getLangOpts().Borland) { |
Francois Pichet | bfaf477 | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 465 | Ident__exception_info->setIsPoisoned(false); |
| 466 | Ident___exception_info->setIsPoisoned(false); |
| 467 | Ident_GetExceptionInfo->setIsPoisoned(false); |
| 468 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 469 | ExprResult FilterExpr(ParseExpression()); |
Francois Pichet | bfaf477 | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 470 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 471 | if (getLangOpts().Borland) { |
Francois Pichet | bfaf477 | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 472 | Ident__exception_info->setIsPoisoned(true); |
| 473 | Ident___exception_info->setIsPoisoned(true); |
| 474 | Ident_GetExceptionInfo->setIsPoisoned(true); |
| 475 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 476 | |
| 477 | if(FilterExpr.isInvalid()) |
| 478 | return StmtError(); |
| 479 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 480 | if (ExpectAndConsume(tok::r_paren)) |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 481 | return StmtError(); |
| 482 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 483 | StmtResult Block(ParseCompoundStatement()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 484 | |
| 485 | if(Block.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 486 | return Block; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 487 | |
| 488 | return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take()); |
| 489 | } |
| 490 | |
| 491 | /// ParseSEHFinallyBlock - Handle __finally |
| 492 | /// |
| 493 | /// seh-finally-block: |
| 494 | /// '__finally' compound-statement |
| 495 | /// |
| 496 | StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) { |
| 497 | PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false), |
| 498 | raii2(Ident___abnormal_termination, false), |
| 499 | raii3(Ident_AbnormalTermination, false); |
| 500 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 501 | StmtResult Block(ParseCompoundStatement()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 502 | if(Block.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 503 | return Block; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 504 | |
| 505 | return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 508 | /// ParseLabeledStatement - We have an identifier and a ':' after it. |
Chris Lattner | 6dfd978 | 2006-08-10 18:31:37 +0000 | [diff] [blame] | 509 | /// |
| 510 | /// labeled-statement: |
| 511 | /// identifier ':' statement |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 512 | /// [GNU] identifier ':' attributes[opt] statement |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 513 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 514 | StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 515 | assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && |
| 516 | "Not an identifier!"); |
| 517 | |
| 518 | Token IdentTok = Tok; // Save the whole token. |
| 519 | ConsumeToken(); // eat the identifier. |
| 520 | |
| 521 | assert(Tok.is(tok::colon) && "Not a label!"); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 522 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 523 | // identifier ':' statement |
| 524 | SourceLocation ColonLoc = ConsumeToken(); |
| 525 | |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 526 | // Read label attributes, if present. |
| 527 | StmtResult SubStmt; |
| 528 | if (Tok.is(tok::kw___attribute)) { |
| 529 | ParsedAttributesWithRange TempAttrs(AttrFactory); |
| 530 | ParseGNUAttributes(TempAttrs); |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 531 | |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 532 | // In C++, GNU attributes only apply to the label if they are followed by a |
| 533 | // semicolon, to disambiguate label attributes from attributes on a labeled |
| 534 | // declaration. |
| 535 | // |
| 536 | // This doesn't quite match what GCC does; if the attribute list is empty |
| 537 | // and followed by a semicolon, GCC will reject (it appears to parse the |
| 538 | // attributes as part of a statement in that case). That looks like a bug. |
| 539 | if (!getLangOpts().CPlusPlus || Tok.is(tok::semi)) |
| 540 | attrs.takeAllFrom(TempAttrs); |
| 541 | else if (isDeclarationStatement()) { |
| 542 | StmtVector Stmts; |
| 543 | // FIXME: We should do this whether or not we have a declaration |
| 544 | // statement, but that doesn't work correctly (because ProhibitAttributes |
| 545 | // can't handle GNU attributes), so only call it in the one case where |
| 546 | // GNU attributes are allowed. |
| 547 | SubStmt = ParseStatementOrDeclarationAfterAttributes( |
| 548 | Stmts, /*OnlyStmts*/ true, 0, TempAttrs); |
| 549 | if (!TempAttrs.empty() && !SubStmt.isInvalid()) |
| 550 | SubStmt = Actions.ProcessStmtAttributes( |
| 551 | SubStmt.get(), TempAttrs.getList(), TempAttrs.Range); |
| 552 | } else { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 553 | Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi; |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | // If we've not parsed a statement yet, parse one now. |
| 558 | if (!SubStmt.isInvalid() && !SubStmt.isUsable()) |
| 559 | SubStmt = ParseStatement(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 560 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 561 | // Broken substmt shouldn't prevent the label from being added to the AST. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 562 | if (SubStmt.isInvalid()) |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 563 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 564 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 565 | LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(), |
| 566 | IdentTok.getLocation()); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 567 | if (AttributeList *Attrs = attrs.getList()) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 568 | Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 569 | attrs.clear(); |
| 570 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 571 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 572 | return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc, |
| 573 | SubStmt.get()); |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 574 | } |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 575 | |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 576 | /// ParseCaseStatement |
| 577 | /// labeled-statement: |
| 578 | /// 'case' constant-expression ':' statement |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 579 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
Chris Lattner | 8693a51 | 2006-08-13 21:54:02 +0000 | [diff] [blame] | 580 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 581 | StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) { |
Richard Smith | d4257d8 | 2011-04-21 22:48:40 +0000 | [diff] [blame] | 582 | assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 584 | // It is very very common for code to contain many case statements recursively |
| 585 | // nested, as in (but usually without indentation): |
| 586 | // case 1: |
| 587 | // case 2: |
| 588 | // case 3: |
| 589 | // case 4: |
| 590 | // case 5: etc. |
| 591 | // |
| 592 | // Parsing this naively works, but is both inefficient and can cause us to run |
| 593 | // out of stack space in our recursive descent parser. As a special case, |
Chris Lattner | 2b19a658 | 2009-03-04 18:24:58 +0000 | [diff] [blame] | 594 | // flatten this recursion into an iterative loop. This is complex and gross, |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 595 | // but all the grossness is constrained to ParseCaseStatement (and some |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 596 | // weirdness in the actions), so this is just local grossness :). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 598 | // TopLevelCase - This is the highest level we have parsed. 'case 1' in the |
| 599 | // example above. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 600 | StmtResult TopLevelCase(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 602 | // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which |
| 603 | // gets updated each time a new case is parsed, and whose body is unset so |
| 604 | // far. When parsing 'case 4', this is the 'case 3' node. |
Richard Trieu | 3481fcd | 2011-09-09 02:16:15 +0000 | [diff] [blame] | 605 | Stmt *DeepestParsedCaseStmt = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 607 | // While we have case statements, eat and stack them. |
David Majnemer | 0ac67fa | 2011-06-13 05:50:12 +0000 | [diff] [blame] | 608 | SourceLocation ColonLoc; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 609 | do { |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 610 | SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() : |
| 611 | ConsumeToken(); // eat the 'case'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 613 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 614 | Actions.CodeCompleteCase(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 615 | cutOffParsing(); |
| 616 | return StmtError(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 617 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 125c0ee | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 619 | /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'. |
| 620 | /// Disable this form of error recovery while we're parsing the case |
| 621 | /// expression. |
| 622 | ColonProtectionRAIIObject ColonProtection(*this); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 623 | |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 624 | ExprResult LHS(MissingCase ? Expr : ParseConstantExpression()); |
| 625 | MissingCase = false; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 626 | if (LHS.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 627 | SkipUntil(tok::colon, StopAtSemi); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 628 | return StmtError(); |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 629 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 631 | // GNU case range extension. |
| 632 | SourceLocation DotDotDotLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 633 | ExprResult RHS; |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 634 | if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) { |
| 635 | Diag(DotDotDotLoc, diag::ext_gnu_case_range); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 636 | RHS = ParseConstantExpression(); |
| 637 | if (RHS.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 638 | SkipUntil(tok::colon, StopAtSemi); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 639 | return StmtError(); |
| 640 | } |
| 641 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 125c0ee | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 643 | ColonProtection.restore(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 644 | |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 645 | if (TryConsumeToken(tok::colon, ColonLoc)) { |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame^] | 646 | } else if (TryConsumeToken(tok::semi, ColonLoc) || |
| 647 | TryConsumeToken(tok::coloncolon, ColonLoc)) { |
| 648 | // Treat "case blah;" or "case blah::" as a typo for "case blah:". |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 649 | Diag(ColonLoc, diag::err_expected_after) |
| 650 | << "'case'" << tok::colon |
| 651 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
John McCall | 0140bfe | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 652 | } else { |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 653 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 654 | Diag(ExpectedLoc, diag::err_expected_after) |
| 655 | << "'case'" << tok::colon |
| 656 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 657 | ColonLoc = ExpectedLoc; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 658 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 659 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 660 | StmtResult Case = |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 661 | Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc, |
| 662 | RHS.get(), ColonLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 664 | // If we had a sema error parsing this case, then just ignore it and |
| 665 | // continue parsing the sub-stmt. |
| 666 | if (Case.isInvalid()) { |
| 667 | if (TopLevelCase.isInvalid()) // No parsed case stmts. |
| 668 | return ParseStatement(); |
| 669 | // Otherwise, just don't add it as a nested case. |
| 670 | } else { |
| 671 | // If this is the first case statement we parsed, it becomes TopLevelCase. |
| 672 | // Otherwise we link it into the current chain. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 673 | Stmt *NextDeepest = Case.get(); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 674 | if (TopLevelCase.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 675 | TopLevelCase = Case; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 676 | else |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 677 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get()); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 678 | DeepestParsedCaseStmt = NextDeepest; |
| 679 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 681 | // Handle all case statements. |
| 682 | } while (Tok.is(tok::kw_case)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 684 | assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 686 | // If we found a non-case statement, start by parsing it. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 687 | StmtResult SubStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 689 | if (Tok.isNot(tok::r_brace)) { |
| 690 | SubStmt = ParseStatement(); |
| 691 | } else { |
| 692 | // Nicely diagnose the common error "switch (X) { case 4: }", which is |
| 693 | // not valid. |
David Majnemer | c6a9987 | 2011-06-14 15:24:38 +0000 | [diff] [blame] | 694 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 695 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
| 696 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 697 | SubStmt = true; |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 698 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 700 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 701 | if (SubStmt.isInvalid()) |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 702 | SubStmt = Actions.ActOnNullStmt(SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 704 | // Install the body into the most deeply-nested case. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 705 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get()); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 707 | // Return the top level parsed statement tree. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 708 | return TopLevelCase; |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /// ParseDefaultStatement |
| 712 | /// labeled-statement: |
| 713 | /// 'default' ':' statement |
| 714 | /// Note that this does not parse the 'statement' at the end. |
| 715 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 716 | StmtResult Parser::ParseDefaultStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 717 | assert(Tok.is(tok::kw_default) && "Not a default stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 718 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 719 | |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 720 | SourceLocation ColonLoc; |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 721 | if (TryConsumeToken(tok::colon, ColonLoc)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 722 | } else if (TryConsumeToken(tok::semi, ColonLoc)) { |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 723 | // Treat "default;" as a typo for "default:". |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 724 | Diag(ColonLoc, diag::err_expected_after) |
| 725 | << "'default'" << tok::colon |
| 726 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
John McCall | 0140bfe | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 727 | } else { |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 728 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 729 | Diag(ExpectedLoc, diag::err_expected_after) |
| 730 | << "'default'" << tok::colon |
| 731 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 732 | ColonLoc = ExpectedLoc; |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 733 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 734 | |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 735 | StmtResult SubStmt; |
| 736 | |
| 737 | if (Tok.isNot(tok::r_brace)) { |
| 738 | SubStmt = ParseStatement(); |
| 739 | } else { |
| 740 | // Diagnose the common error "switch (X) {... default: }", which is |
| 741 | // not valid. |
David Majnemer | c6a9987 | 2011-06-14 15:24:38 +0000 | [diff] [blame] | 742 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 743 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
| 744 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
| 745 | SubStmt = true; |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 748 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 749 | if (SubStmt.isInvalid()) |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 750 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 751 | |
Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 752 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 753 | SubStmt.get(), getCurScope()); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 756 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
| 757 | return ParseCompoundStatement(isStmtExpr, Scope::DeclScope); |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 758 | } |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 759 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 760 | /// ParseCompoundStatement - Parse a "{}" block. |
| 761 | /// |
| 762 | /// compound-statement: [C99 6.8.2] |
| 763 | /// { block-item-list[opt] } |
| 764 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 765 | /// |
| 766 | /// block-item-list: |
| 767 | /// block-item |
| 768 | /// block-item-list block-item |
| 769 | /// |
| 770 | /// block-item: |
| 771 | /// declaration |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 772 | /// [GNU] '__extension__' declaration |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 773 | /// statement |
| 774 | /// [OMP] openmp-directive [TODO] |
| 775 | /// |
| 776 | /// [GNU] label-declarations: |
| 777 | /// [GNU] label-declaration |
| 778 | /// [GNU] label-declarations label-declaration |
| 779 | /// |
| 780 | /// [GNU] label-declaration: |
| 781 | /// [GNU] '__label__' identifier-list ';' |
| 782 | /// |
| 783 | /// [OMP] openmp-directive: [TODO] |
| 784 | /// [OMP] barrier-directive |
| 785 | /// [OMP] flush-directive |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 786 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 787 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 788 | unsigned ScopeFlags) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 789 | assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 791 | // Enter a scope to hold everything within the compound stmt. Compound |
| 792 | // statements can always hold declarations. |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 793 | ParseScope CompoundScope(this, ScopeFlags); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 794 | |
| 795 | // Parse the statements in the body. |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 796 | return ParseCompoundStatementBody(isStmtExpr); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 799 | /// Parse any pragmas at the start of the compound expression. We handle these |
| 800 | /// separately since some pragmas (FP_CONTRACT) must appear before any C |
| 801 | /// statement in the compound, but may be intermingled with other pragmas. |
| 802 | void Parser::ParseCompoundStatementLeadingPragmas() { |
| 803 | bool checkForPragmas = true; |
| 804 | while (checkForPragmas) { |
| 805 | switch (Tok.getKind()) { |
| 806 | case tok::annot_pragma_vis: |
| 807 | HandlePragmaVisibility(); |
| 808 | break; |
| 809 | case tok::annot_pragma_pack: |
| 810 | HandlePragmaPack(); |
| 811 | break; |
| 812 | case tok::annot_pragma_msstruct: |
| 813 | HandlePragmaMSStruct(); |
| 814 | break; |
| 815 | case tok::annot_pragma_align: |
| 816 | HandlePragmaAlign(); |
| 817 | break; |
| 818 | case tok::annot_pragma_weak: |
| 819 | HandlePragmaWeak(); |
| 820 | break; |
| 821 | case tok::annot_pragma_weakalias: |
| 822 | HandlePragmaWeakAlias(); |
| 823 | break; |
| 824 | case tok::annot_pragma_redefine_extname: |
| 825 | HandlePragmaRedefineExtname(); |
| 826 | break; |
| 827 | case tok::annot_pragma_opencl_extension: |
| 828 | HandlePragmaOpenCLExtension(); |
| 829 | break; |
| 830 | case tok::annot_pragma_fp_contract: |
| 831 | HandlePragmaFPContract(); |
| 832 | break; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 833 | case tok::annot_pragma_ms_pointers_to_members: |
| 834 | HandlePragmaMSPointersToMembers(); |
| 835 | break; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 836 | case tok::annot_pragma_ms_pragma: |
| 837 | HandlePragmaMSPragma(); |
| 838 | break; |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 839 | default: |
| 840 | checkForPragmas = false; |
| 841 | break; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | } |
| 846 | |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 847 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 848 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 849 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 850 | /// stack. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 851 | StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 853 | Tok.getLocation(), |
| 854 | "in compound statement ('{}')"); |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 855 | |
| 856 | // Record the state of the FP_CONTRACT pragma, restore on leaving the |
| 857 | // compound statement. |
| 858 | Sema::FPContractStateRAII SaveFPContractState(Actions); |
| 859 | |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 860 | InMessageExpressionRAIIObject InMessage(*this, false); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 861 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 862 | if (T.consumeOpen()) |
| 863 | return StmtError(); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 864 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 865 | Sema::CompoundScopeRAII CompoundScope(Actions); |
| 866 | |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 867 | // Parse any pragmas at the beginning of the compound statement. |
| 868 | ParseCompoundStatementLeadingPragmas(); |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 869 | |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 870 | StmtVector Stmts; |
Lang Hames | a930e71 | 2012-10-21 01:10:01 +0000 | [diff] [blame] | 871 | |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 872 | // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are |
| 873 | // only allowed at the start of a compound stmt regardless of the language. |
| 874 | while (Tok.is(tok::kw___label__)) { |
| 875 | SourceLocation LabelLoc = ConsumeToken(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 876 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 877 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 878 | while (1) { |
| 879 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 880 | Diag(Tok, diag::err_expected) << tok::identifier; |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 881 | break; |
| 882 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 884 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 885 | SourceLocation IdLoc = ConsumeToken(); |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 886 | DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 887 | |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 888 | if (!TryConsumeToken(tok::comma)) |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 889 | break; |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 890 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 891 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 892 | DeclSpec DS(AttrFactory); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 893 | DeclGroupPtrTy Res = |
| 894 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 895 | StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 897 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 898 | if (R.isUsable()) |
| 899 | Stmts.push_back(R.release()); |
| 900 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 901 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 902 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 903 | if (Tok.is(tok::annot_pragma_unused)) { |
| 904 | HandlePragmaUnused(); |
| 905 | continue; |
| 906 | } |
| 907 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 908 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 909 | Tok.is(tok::kw___if_not_exists))) { |
| 910 | ParseMicrosoftIfExistsStatement(Stmts); |
| 911 | continue; |
| 912 | } |
| 913 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 914 | StmtResult R; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 915 | if (Tok.isNot(tok::kw___extension__)) { |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 916 | R = ParseStatementOrDeclaration(Stmts, false); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 917 | } else { |
| 918 | // __extension__ can start declarations and it can also be a unary |
| 919 | // operator for expressions. Consume multiple __extension__ markers here |
| 920 | // until we can determine which is which. |
Eli Friedman | eb3a9b0 | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 921 | // FIXME: This loses extension expressions in the AST! |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 922 | SourceLocation ExtLoc = ConsumeToken(); |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 923 | while (Tok.is(tok::kw___extension__)) |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 924 | ConsumeToken(); |
Chris Lattner | 1ff6e73 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 925 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 926 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 927 | MaybeParseCXX11Attributes(attrs, 0, /*MightBeObjCMessageSend*/ true); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 928 | |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 929 | // If this is the start of a declaration, parse it as such. |
Argyrios Kyrtzidis | 2c7137d | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 930 | if (isDeclarationStatement()) { |
Eli Friedman | 15af3ee | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 931 | // __extension__ silences extension warnings in the subdeclaration. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 932 | // FIXME: Save the __extension__ on the decl as a node somehow? |
Eli Friedman | 15af3ee | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 933 | ExtensionRAIIObject O(Diags); |
| 934 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 935 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 936 | DeclGroupPtrTy Res = ParseDeclaration(Stmts, |
| 937 | Declarator::BlockContext, DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 938 | attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 939 | R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 940 | } else { |
Eli Friedman | eb3a9b0 | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 941 | // Otherwise this was a unary __extension__ marker. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 942 | ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); |
Chris Lattner | fdc0748 | 2008-03-13 06:32:11 +0000 | [diff] [blame] | 943 | |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 944 | if (Res.isInvalid()) { |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 945 | SkipUntil(tok::semi); |
| 946 | continue; |
| 947 | } |
Sebastian Redl | c2edafb | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 948 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 949 | // FIXME: Use attributes? |
Chris Lattner | 1ff6e73 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 950 | // Eat the semicolon at the end of stmt and convert the expr into a |
| 951 | // statement. |
Douglas Gregor | 45d6bdf | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 952 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 953 | R = Actions.ActOnExprStmt(Res); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 956 | |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 957 | if (R.isUsable()) |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 958 | Stmts.push_back(R.release()); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 959 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 960 | |
Argyrios Kyrtzidis | 6db8501 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 961 | SourceLocation CloseLoc = Tok.getLocation(); |
| 962 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 963 | // We broke out of the while loop because we found a '}' or EOF. |
Nico Weber | a48b6c2 | 2012-12-30 23:36:56 +0000 | [diff] [blame] | 964 | if (!T.consumeClose()) |
Argyrios Kyrtzidis | 6db8501 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 965 | // Recover by creating a compound statement with what we parsed so far, |
| 966 | // instead of dropping everything and returning StmtError(); |
Nico Weber | a48b6c2 | 2012-12-30 23:36:56 +0000 | [diff] [blame] | 967 | CloseLoc = T.getCloseLocation(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 968 | |
Argyrios Kyrtzidis | 6db8501 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 969 | return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 970 | Stmts, isStmtExpr); |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 971 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 972 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 973 | /// ParseParenExprOrCondition: |
| 974 | /// [C ] '(' expression ')' |
Chris Lattner | 10da53c | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 975 | /// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true] |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 976 | /// |
| 977 | /// This function parses and performs error recovery on the specified condition |
| 978 | /// or expression (depending on whether we're in C++ or C mode). This function |
| 979 | /// goes out of its way to recover well. It returns true if there was a parser |
| 980 | /// error (the right paren couldn't be found), which indicates that the caller |
| 981 | /// should try to recover harder. It returns false if the condition is |
| 982 | /// successfully parsed. Note that a successful parse can still have semantic |
| 983 | /// errors in the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 984 | bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 985 | Decl *&DeclResult, |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 986 | SourceLocation Loc, |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 987 | bool ConvertToBoolean) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 988 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 989 | T.consumeOpen(); |
| 990 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 991 | if (getLangOpts().CPlusPlus) |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 992 | ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 993 | else { |
| 994 | ExprResult = ParseExpression(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 995 | DeclResult = 0; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 996 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 997 | // If required, convert to a boolean value. |
| 998 | if (!ExprResult.isInvalid() && ConvertToBoolean) |
| 999 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1000 | = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get()); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1001 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1003 | // If the parser was confused by the condition and we don't have a ')', try to |
| 1004 | // recover by skipping ahead to a semi and bailing out. If condexp is |
| 1005 | // semantically invalid but we have well formed code, keep going. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1006 | if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) { |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1007 | SkipUntil(tok::semi); |
| 1008 | // Skipping may have stopped if it found the containing ')'. If so, we can |
| 1009 | // continue parsing the if statement. |
| 1010 | if (Tok.isNot(tok::r_paren)) |
| 1011 | return true; |
| 1012 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1014 | // Otherwise the condition is valid or the rparen is present. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1015 | T.consumeClose(); |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 70d4498 | 2012-04-28 16:24:20 +0000 | [diff] [blame] | 1017 | // Check for extraneous ')'s to catch things like "if (foo())) {". We know |
| 1018 | // that all callers are looking for a statement after the condition, so ")" |
| 1019 | // isn't valid. |
| 1020 | while (Tok.is(tok::r_paren)) { |
| 1021 | Diag(Tok, diag::err_extraneous_rparen_in_condition) |
| 1022 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1023 | ConsumeParen(); |
| 1024 | } |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1030 | /// ParseIfStatement |
| 1031 | /// if-statement: [C99 6.8.4.1] |
| 1032 | /// 'if' '(' expression ')' statement |
| 1033 | /// 'if' '(' expression ')' statement 'else' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1034 | /// [C++] 'if' '(' condition ')' statement |
| 1035 | /// [C++] 'if' '(' condition ')' statement 'else' statement |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1036 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1037 | StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1038 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1039 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1041 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1042 | Diag(Tok, diag::err_expected_lparen_after) << "if"; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1043 | SkipUntil(tok::semi); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1044 | return StmtError(); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1045 | } |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1046 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1047 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1049 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 1050 | // the case for C90. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1051 | // |
| 1052 | // C++ 6.4p3: |
| 1053 | // A name introduced by a declaration in a condition is in scope from its |
| 1054 | // point of declaration until the end of the substatements controlled by the |
| 1055 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1056 | // C++ 3.3.2p4: |
| 1057 | // Names declared in the for-init-statement, and in the condition of if, |
| 1058 | // while, for, and switch statements are local to the if, while, for, or |
| 1059 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1060 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1061 | ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1062 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1063 | // Parse the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1064 | ExprResult CondExp; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1065 | Decl *CondVar = 0; |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 1066 | if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true)) |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1067 | return StmtError(); |
Chris Lattner | bc2d77c | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 1068 | |
David Blaikie | a5696df | 2012-05-16 04:20:04 +0000 | [diff] [blame] | 1069 | FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1071 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1072 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1073 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1074 | // |
| 1075 | // C++ 6.4p1: |
| 1076 | // The substatement in a selection-statement (each substatement, in the else |
| 1077 | // form of the if statement) implicitly defines a local scope. |
| 1078 | // |
| 1079 | // For C++ we create a scope for the condition and a new scope for |
| 1080 | // substatements because: |
| 1081 | // -When the 'then' scope exits, we want the condition declaration to still be |
| 1082 | // active for the 'else' scope too. |
| 1083 | // -Sema will detect name clashes by considering declarations of a |
| 1084 | // 'ControlScope' as part of its direct subscope. |
| 1085 | // -If we wanted the condition and substatement to be in the same scope, we |
| 1086 | // would have to notify ParseStatement not to create a new scope. It's |
| 1087 | // simpler to let it create a new scope. |
| 1088 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1089 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1091 | // Read the 'then' stmt. |
| 1092 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1093 | |
| 1094 | SourceLocation InnerStatementTrailingElseLoc; |
| 1095 | StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc)); |
Chris Lattner | ac4471c | 2007-05-28 05:38:24 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 37e54f4 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 1097 | // Pop the 'if' scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1098 | InnerScope.Exit(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1100 | // If it has an else, parse it. |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1101 | SourceLocation ElseLoc; |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1102 | SourceLocation ElseStmtLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1103 | StmtResult ElseStmt; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1105 | if (Tok.is(tok::kw_else)) { |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1106 | if (TrailingElseLoc) |
| 1107 | *TrailingElseLoc = Tok.getLocation(); |
| 1108 | |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1109 | ElseLoc = ConsumeToken(); |
Chris Lattner | df74264 | 2010-04-12 06:12:50 +0000 | [diff] [blame] | 1110 | ElseStmtLoc = Tok.getLocation(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1112 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1113 | // there is no compound stmt. C90 does not have this clause. We only do |
| 1114 | // this if the body isn't a compound statement to avoid push/pop in common |
| 1115 | // cases. |
Argyrios Kyrtzidis | 504bb84 | 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 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1121 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1123 | ElseStmt = ParseStatement(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | 37e54f4 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 1125 | // Pop the 'else' scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1126 | InnerScope.Exit(); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 1127 | } else if (Tok.is(tok::code_completion)) { |
| 1128 | Actions.CodeCompleteAfterIf(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1129 | cutOffParsing(); |
| 1130 | return StmtError(); |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1131 | } else if (InnerStatementTrailingElseLoc.isValid()) { |
| 1132 | Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1133 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1135 | IfScope.Exit(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1137 | // If the then or else stmt is invalid and the other is valid (and present), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | // make turn the invalid one into a null stmt to avoid dropping the other |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1139 | // part. If both are invalid, return error. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1140 | if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || |
| 1141 | (ThenStmt.isInvalid() && ElseStmt.get() == 0) || |
| 1142 | (ThenStmt.get() == 0 && ElseStmt.isInvalid())) { |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1143 | // Both invalid, or one is invalid and other is non-present: return error. |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1144 | return StmtError(); |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1145 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1147 | // Now if either are invalid, replace with a ';'. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1148 | if (ThenStmt.isInvalid()) |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1149 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1150 | if (ElseStmt.isInvalid()) |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1151 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1152 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1153 | return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(), |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 1154 | ElseLoc, ElseStmt.get()); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1157 | /// ParseSwitchStatement |
| 1158 | /// switch-statement: |
| 1159 | /// 'switch' '(' expression ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1160 | /// [C++] 'switch' '(' condition ')' statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1161 | StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1162 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1163 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1165 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1166 | Diag(Tok, diag::err_expected_lparen_after) << "switch"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1167 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1168 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1169 | } |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1170 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1171 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1173 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 1174 | // not the case for C90. Start the switch scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1175 | // |
| 1176 | // C++ 6.4p3: |
| 1177 | // A name introduced by a declaration in a condition is in scope from its |
| 1178 | // point of declaration until the end of the substatements controlled by the |
| 1179 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1180 | // C++ 3.3.2p4: |
| 1181 | // Names declared in the for-init-statement, and in the condition of if, |
| 1182 | // while, for, and switch statements are local to the if, while, for, or |
| 1183 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1184 | // |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1185 | unsigned ScopeFlags = Scope::SwitchScope; |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1186 | if (C99orCXX) |
| 1187 | ScopeFlags |= Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1188 | ParseScope SwitchScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1190 | // Parse the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1191 | ExprResult Cond; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1192 | Decl *CondVar = 0; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1193 | if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false)) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1194 | return StmtError(); |
Eli Friedman | 44842d1 | 2008-12-17 22:19:57 +0000 | [diff] [blame] | 1195 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1196 | StmtResult Switch |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1197 | = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1199 | if (Switch.isInvalid()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1200 | // Skip the switch body. |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1201 | // FIXME: This is not optimal recovery, but parsing the body is more |
| 1202 | // dangerous due to the presence of case and default statements, which |
| 1203 | // will have no place to connect back with the switch. |
Douglas Gregor | 4abc32d | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 1204 | if (Tok.is(tok::l_brace)) { |
| 1205 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1206 | SkipUntil(tok::r_brace); |
Douglas Gregor | 4abc32d | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 1207 | } else |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1208 | SkipUntil(tok::semi); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1209 | return Switch; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1210 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1212 | // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1213 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1214 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1215 | // |
| 1216 | // C++ 6.4p1: |
| 1217 | // The substatement in a selection-statement (each substatement, in the else |
| 1218 | // form of the if statement) implicitly defines a local scope. |
| 1219 | // |
| 1220 | // See comments in ParseIfStatement for why we create a scope for the |
| 1221 | // condition and a new scope for substatement in C++. |
| 1222 | // |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1223 | getCurScope()->AddFlags(Scope::BreakScope); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1224 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1226 | // Read the body statement. |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1227 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1229 | // Pop the scopes. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1230 | InnerScope.Exit(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1231 | SwitchScope.Exit(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1232 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1233 | if (Body.isInvalid()) { |
Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1234 | // FIXME: Remove the case statement list from the Switch statement. |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1235 | |
| 1236 | // Put the synthesized null statement on the same line as the end of switch |
| 1237 | // condition. |
| 1238 | SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd(); |
| 1239 | Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation); |
| 1240 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1241 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1242 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /// ParseWhileStatement |
| 1246 | /// while-statement: [C99 6.8.5.1] |
| 1247 | /// 'while' '(' expression ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1248 | /// [C++] 'while' '(' condition ')' statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1249 | StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1250 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1251 | SourceLocation WhileLoc = Tok.getLocation(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1252 | ConsumeToken(); // eat the 'while'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1253 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1254 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1255 | Diag(Tok, diag::err_expected_lparen_after) << "while"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1256 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1257 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1258 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1259 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1260 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1262 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 1263 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1264 | // |
| 1265 | // C++ 6.4p3: |
| 1266 | // A name introduced by a declaration in a condition is in scope from its |
| 1267 | // point of declaration until the end of the substatements controlled by the |
| 1268 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1269 | // C++ 3.3.2p4: |
| 1270 | // Names declared in the for-init-statement, and in the condition of if, |
| 1271 | // while, for, and switch statements are local to the if, while, for, or |
| 1272 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1273 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1274 | unsigned ScopeFlags; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1275 | if (C99orCXX) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1276 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 1277 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1278 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1279 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 1280 | ParseScope WhileScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1281 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1282 | // Parse the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1283 | ExprResult Cond; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1284 | Decl *CondVar = 0; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1285 | if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true)) |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1286 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1287 | |
David Blaikie | a5696df | 2012-05-16 04:20:04 +0000 | [diff] [blame] | 1288 | FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
Justin Bogner | e4ebb6c | 2013-12-03 07:36:55 +0000 | [diff] [blame] | 1290 | // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1291 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1292 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1293 | // |
| 1294 | // C++ 6.5p2: |
| 1295 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1296 | // which is entered and exited each time through the loop. |
| 1297 | // |
| 1298 | // See comments in ParseIfStatement for why we create a scope for the |
| 1299 | // condition and a new scope for substatement in C++. |
| 1300 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1301 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1303 | // Read the body statement. |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1304 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1306 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1307 | InnerScope.Exit(); |
| 1308 | WhileScope.Exit(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1309 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1310 | if ((Cond.isInvalid() && !CondVar) || Body.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1311 | return StmtError(); |
| 1312 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1313 | return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /// ParseDoStatement |
| 1317 | /// do-statement: [C99 6.8.5.2] |
| 1318 | /// 'do' statement 'while' '(' expression ')' ';' |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1319 | /// Note: this lets the caller parse the end ';'. |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1320 | StmtResult Parser::ParseDoStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1321 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1322 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1324 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 1325 | // the case for C90. Start the loop scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1326 | unsigned ScopeFlags; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1327 | if (getLangOpts().C99) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1328 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1329 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1330 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1331 | |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1332 | ParseScope DoScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1333 | |
Justin Bogner | e4ebb6c | 2013-12-03 07:36:55 +0000 | [diff] [blame] | 1334 | // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1335 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1336 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | fea3801 | 2008-09-11 04:46:46 +0000 | [diff] [blame] | 1337 | // |
| 1338 | // C++ 6.5p2: |
| 1339 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1340 | // which is entered and exited each time through the loop. |
| 1341 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1342 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
| 1343 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1345 | // Read the body statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1346 | StmtResult Body(ParseStatement()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1348 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1349 | InnerScope.Exit(); |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1351 | if (Tok.isNot(tok::kw_while)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1352 | if (!Body.isInvalid()) { |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1353 | Diag(Tok, diag::err_expected_while); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1354 | Diag(DoLoc, diag::note_matching) << "'do'"; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1355 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1356 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1357 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1358 | } |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1359 | SourceLocation WhileLoc = ConsumeToken(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1361 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1362 | Diag(Tok, diag::err_expected_lparen_after) << "do/while"; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1363 | SkipUntil(tok::semi, StopBeforeMatch); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1364 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1365 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1366 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 1367 | // Parse the parenthesized expression. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1368 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1369 | T.consumeOpen(); |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1370 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 1371 | // A do-while expression is not a condition, so can't have attributes. |
| 1372 | DiagnoseAndSkipCXX11Attributes(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1373 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1374 | ExprResult Cond = ParseExpression(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1375 | T.consumeClose(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1376 | DoScope.Exit(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1377 | |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1378 | if (Cond.isInvalid() || Body.isInvalid()) |
| 1379 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1380 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1381 | return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(), |
| 1382 | Cond.get(), T.getCloseLocation()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /// ParseForStatement |
| 1386 | /// for-statement: [C99 6.8.5.3] |
| 1387 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 1388 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1389 | /// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')' |
| 1390 | /// [C++] statement |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1391 | /// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1392 | /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement |
| 1393 | /// [OBJC2] 'for' '(' expr 'in' expr ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1394 | /// |
| 1395 | /// [C++] for-init-statement: |
| 1396 | /// [C++] expression-statement |
| 1397 | /// [C++] simple-declaration |
| 1398 | /// |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1399 | /// [C++0x] for-range-declaration: |
| 1400 | /// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1401 | /// [C++0x] for-range-initializer: |
| 1402 | /// [C++0x] expression |
| 1403 | /// [C++0x] braced-init-list [TODO] |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1404 | StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1405 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1406 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1407 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1408 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1409 | Diag(Tok, diag::err_expected_lparen_after) << "for"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1410 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1411 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1412 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1413 | |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1414 | bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || |
| 1415 | getLangOpts().ObjC1; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1417 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 1418 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1419 | // |
| 1420 | // C++ 6.4p3: |
| 1421 | // A name introduced by a declaration in a condition is in scope from its |
| 1422 | // point of declaration until the end of the substatements controlled by the |
| 1423 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1424 | // C++ 3.3.2p4: |
| 1425 | // Names declared in the for-init-statement, and in the condition of if, |
| 1426 | // while, for, and switch statements are local to the if, while, for, or |
| 1427 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1428 | // C++ 6.5.3p1: |
| 1429 | // Names declared in the for-init-statement are in the same declarative-region |
| 1430 | // as those declared in the condition. |
| 1431 | // |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1432 | unsigned ScopeFlags = 0; |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1433 | if (C99orCXXorObjC) |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1434 | ScopeFlags = Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1435 | |
| 1436 | ParseScope ForScope(this, ScopeFlags); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1437 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1438 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1439 | T.consumeOpen(); |
| 1440 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1441 | ExprResult Value; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1442 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1443 | bool ForEach = false, ForRange = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1444 | StmtResult FirstPart; |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1445 | bool SecondPartIsInvalid = false; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1446 | FullExprArg SecondPart(Actions); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1447 | ExprResult Collection; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1448 | ForRangeInit ForRangeInit; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1449 | FullExprArg ThirdPart(Actions); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1450 | Decl *SecondVar = 0; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1451 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1452 | if (Tok.is(tok::code_completion)) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1453 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1454 | C99orCXXorObjC? Sema::PCC_ForInit |
| 1455 | : Sema::PCC_Expression); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1456 | cutOffParsing(); |
| 1457 | return StmtError(); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1458 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1459 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1460 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1461 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1463 | // Parse the first part of the for specifier. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1464 | if (Tok.is(tok::semi)) { // for (; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1465 | ProhibitAttributes(attrs); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1466 | // no first part, eat the ';'. |
| 1467 | ConsumeToken(); |
Eli Friedman | 0ffc31c | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 1468 | } else if (isForInitDeclaration()) { // for (int X = 4; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1469 | // Parse declaration, which eats the ';'. |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1470 | if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode? |
Chris Lattner | ab180365 | 2006-08-10 05:22:36 +0000 | [diff] [blame] | 1471 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1472 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1473 | // In C++0x, "for (T NS:a" might not be a typo for :: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1474 | bool MightBeForRangeStmt = getLangOpts().CPlusPlus; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1475 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
| 1476 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1477 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1478 | StmtVector Stmts; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1479 | DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1480 | DeclEnd, attrs, false, |
| 1481 | MightBeForRangeStmt ? |
| 1482 | &ForRangeInit : 0); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1483 | FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1485 | if (ForRangeInit.ParsedForRangeDecl()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1486 | Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1487 | diag::warn_cxx98_compat_for_range : diag::ext_for_range); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 1488 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1489 | ForRange = true; |
| 1490 | } else if (Tok.is(tok::semi)) { // for (int x = 4; |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1491 | ConsumeToken(); |
| 1492 | } else if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 1493 | Actions.ActOnForEachDeclStmt(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | // ObjC: for (id x in expr) |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1495 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1497 | if (Tok.is(tok::code_completion)) { |
| 1498 | Actions.CodeCompleteObjCForCollection(getCurScope(), DG); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1499 | cutOffParsing(); |
| 1500 | return StmtError(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1501 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1502 | Collection = ParseExpression(); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1503 | } else { |
| 1504 | Diag(Tok, diag::err_expected_semi_for); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1505 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1506 | } else { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1507 | ProhibitAttributes(attrs); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 1508 | Value = ParseExpression(); |
Chris Lattner | 71e23ce | 2006-11-04 20:18:38 +0000 | [diff] [blame] | 1509 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1510 | ForEach = isTokIdentifier_in(); |
| 1511 | |
Chris Lattner | cd68f64 | 2007-06-27 01:06:29 +0000 | [diff] [blame] | 1512 | // Turn the expression into a stmt. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1513 | if (!Value.isInvalid()) { |
| 1514 | if (ForEach) |
| 1515 | FirstPart = Actions.ActOnForEachLValueExpr(Value.get()); |
| 1516 | else |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1517 | FirstPart = Actions.ActOnExprStmt(Value); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1518 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1520 | if (Tok.is(tok::semi)) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1521 | ConsumeToken(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1522 | } else if (ForEach) { |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1523 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1525 | if (Tok.is(tok::code_completion)) { |
| 1526 | Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1527 | cutOffParsing(); |
| 1528 | return StmtError(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1529 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1530 | Collection = ParseExpression(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1531 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) { |
Richard Smith | 4f848f1 | 2011-12-20 22:56:20 +0000 | [diff] [blame] | 1532 | // User tried to write the reasonable, but ill-formed, for-range-statement |
| 1533 | // for (expr : expr) { ... } |
| 1534 | Diag(Tok, diag::err_for_range_expected_decl) |
| 1535 | << FirstPart.get()->getSourceRange(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1536 | SkipUntil(tok::r_paren, StopBeforeMatch); |
Richard Smith | 4f848f1 | 2011-12-20 22:56:20 +0000 | [diff] [blame] | 1537 | SecondPartIsInvalid = true; |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1538 | } else { |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1539 | if (!Value.isInvalid()) { |
| 1540 | Diag(Tok, diag::err_expected_semi_for); |
| 1541 | } else { |
| 1542 | // Skip until semicolon or rparen, don't consume it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1543 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1544 | if (Tok.is(tok::semi)) |
| 1545 | ConsumeToken(); |
| 1546 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1547 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1548 | } |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1549 | |
| 1550 | // Parse the second part of the for specifier. |
| 1551 | getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1552 | if (!ForEach && !ForRange) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1553 | assert(!SecondPart.get() && "Shouldn't have a second expression yet."); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1554 | // Parse the second part of the for specifier. |
| 1555 | if (Tok.is(tok::semi)) { // for (...;; |
| 1556 | // no second part. |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1557 | } else if (Tok.is(tok::r_paren)) { |
| 1558 | // missing both semicolons. |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1559 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1560 | ExprResult Second; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1561 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1562 | ParseCXXCondition(Second, SecondVar, ForLoc, true); |
| 1563 | else { |
| 1564 | Second = ParseExpression(); |
| 1565 | if (!Second.isInvalid()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1566 | Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1567 | Second.get()); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1568 | } |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1569 | SecondPartIsInvalid = Second.isInvalid(); |
David Blaikie | a5696df | 2012-05-16 04:20:04 +0000 | [diff] [blame] | 1570 | SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1571 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1572 | |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1573 | if (Tok.isNot(tok::semi)) { |
| 1574 | if (!SecondPartIsInvalid || SecondVar) |
| 1575 | Diag(Tok, diag::err_expected_semi_for); |
| 1576 | else |
| 1577 | // Skip until semicolon or rparen, don't consume it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1578 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1581 | if (Tok.is(tok::semi)) { |
| 1582 | ConsumeToken(); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1583 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1584 | |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1585 | // Parse the third part of the for specifier. |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1586 | if (Tok.isNot(tok::r_paren)) { // for (...;...;) |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1587 | ExprResult Third = ParseExpression(); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1588 | // FIXME: The C++11 standard doesn't actually say that this is a |
| 1589 | // discarded-value expression, but it clearly should be. |
| 1590 | ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.take()); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1591 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1592 | } |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 1593 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1594 | T.consumeClose(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1595 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1596 | // We need to perform most of the semantic analysis for a C++0x for-range |
| 1597 | // statememt before parsing the body, in order to be able to deduce the type |
| 1598 | // of an auto-typed loop variable. |
| 1599 | StmtResult ForRangeStmt; |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1600 | StmtResult ForEachStmt; |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1601 | |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1602 | if (ForRange) { |
Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1603 | ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(), |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1604 | ForRangeInit.ColonLoc, |
| 1605 | ForRangeInit.RangeExpr.get(), |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 1606 | T.getCloseLocation(), |
| 1607 | Sema::BFRK_Build); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1608 | |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Similarly, we need to do the semantic analysis for a for-range |
| 1611 | // statement immediately in order to close over temporaries correctly. |
| 1612 | } else if (ForEach) { |
Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1613 | ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc, |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1614 | FirstPart.take(), |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1615 | Collection.take(), |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1616 | T.getCloseLocation()); |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Justin Bogner | e4ebb6c | 2013-12-03 07:36:55 +0000 | [diff] [blame] | 1619 | // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1620 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1621 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1622 | // |
| 1623 | // C++ 6.5p2: |
| 1624 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1625 | // which is entered and exited each time through the loop. |
| 1626 | // |
| 1627 | // See comments in ParseIfStatement for why we create a scope for |
| 1628 | // for-init-statement/condition and a new scope for substatement in C++. |
| 1629 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1630 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC, |
| 1631 | Tok.is(tok::l_brace)); |
| 1632 | |
| 1633 | // The body of the for loop has the same local mangling number as the |
| 1634 | // for-init-statement. |
| 1635 | // It will only be incremented if the body contains other things that would |
| 1636 | // normally increment the mangling number (like a compound statement). |
| 1637 | if (C99orCXXorObjC) |
| 1638 | getCurScope()->decrementMSLocalManglingNumber(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1640 | // Read the body statement. |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1641 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1642 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1643 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1644 | InnerScope.Exit(); |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1646 | // Leave the for-scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1647 | ForScope.Exit(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1648 | |
| 1649 | if (Body.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1650 | return StmtError(); |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1651 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1652 | if (ForEach) |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1653 | return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(), |
| 1654 | Body.take()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1656 | if (ForRange) |
| 1657 | return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take()); |
| 1658 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1659 | return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(), |
| 1660 | SecondPart, SecondVar, ThirdPart, |
| 1661 | T.getCloseLocation(), Body.take()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1662 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1664 | /// ParseGotoStatement |
| 1665 | /// jump-statement: |
| 1666 | /// 'goto' identifier ';' |
| 1667 | /// [GNU] 'goto' '*' expression ';' |
| 1668 | /// |
| 1669 | /// Note: this lets the caller parse the end ';'. |
| 1670 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1671 | StmtResult Parser::ParseGotoStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1672 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1673 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1674 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1675 | StmtResult Res; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1676 | if (Tok.is(tok::identifier)) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1677 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
| 1678 | Tok.getLocation()); |
| 1679 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1680 | ConsumeToken(); |
Eli Friedman | 5d72d41 | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 1681 | } else if (Tok.is(tok::star)) { |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1682 | // GNU indirect goto extension. |
| 1683 | Diag(Tok, diag::ext_gnu_indirect_goto); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1684 | SourceLocation StarLoc = ConsumeToken(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1685 | ExprResult R(ParseExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1686 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1687 | SkipUntil(tok::semi, StopBeforeMatch); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1688 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1689 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1690 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take()); |
Chris Lattner | e34b2c2 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 1691 | } else { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1692 | Diag(Tok, diag::err_expected) << tok::identifier; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1693 | return StmtError(); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1694 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1695 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1696 | return Res; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1699 | /// ParseContinueStatement |
| 1700 | /// jump-statement: |
| 1701 | /// 'continue' ';' |
| 1702 | /// |
| 1703 | /// Note: this lets the caller parse the end ';'. |
| 1704 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1705 | StmtResult Parser::ParseContinueStatement() { |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1706 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1707 | return Actions.ActOnContinueStmt(ContinueLoc, getCurScope()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /// ParseBreakStatement |
| 1711 | /// jump-statement: |
| 1712 | /// 'break' ';' |
| 1713 | /// |
| 1714 | /// Note: this lets the caller parse the end ';'. |
| 1715 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1716 | StmtResult Parser::ParseBreakStatement() { |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1717 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1718 | return Actions.ActOnBreakStmt(BreakLoc, getCurScope()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1721 | /// ParseReturnStatement |
| 1722 | /// jump-statement: |
| 1723 | /// 'return' expression[opt] ';' |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1724 | StmtResult Parser::ParseReturnStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1725 | assert(Tok.is(tok::kw_return) && "Not a return stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1726 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1727 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1728 | ExprResult R; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1729 | if (Tok.isNot(tok::semi)) { |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1730 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1731 | Actions.CodeCompleteReturn(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1732 | cutOffParsing(); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1733 | return StmtError(); |
| 1734 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1735 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1736 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) { |
Douglas Gregor | e9e27d9 | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1737 | R = ParseInitializer(); |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1738 | if (R.isUsable()) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1739 | Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1740 | diag::warn_cxx98_compat_generalized_initializer_lists : |
| 1741 | diag::ext_generalized_initializer_lists) |
Douglas Gregor | e9e27d9 | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1742 | << R.get()->getSourceRange(); |
| 1743 | } else |
| 1744 | R = ParseExpression(); |
Serge Pavlov | f79bd5c | 2013-12-04 03:51:59 +0000 | [diff] [blame] | 1745 | if (R.isInvalid()) { |
| 1746 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1747 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1748 | } |
Chris Lattner | a0927ce | 2006-08-12 16:59:03 +0000 | [diff] [blame] | 1749 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1750 | return Actions.ActOnReturnStmt(ReturnLoc, R.take()); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1751 | } |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1752 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1753 | namespace { |
| 1754 | class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback { |
| 1755 | Parser &TheParser; |
| 1756 | SourceLocation AsmLoc; |
| 1757 | StringRef AsmString; |
| 1758 | |
| 1759 | /// The tokens we streamed into AsmString and handed off to MC. |
| 1760 | ArrayRef<Token> AsmToks; |
| 1761 | |
| 1762 | /// The offset of each token in AsmToks within AsmString. |
| 1763 | ArrayRef<unsigned> AsmTokOffsets; |
| 1764 | |
| 1765 | public: |
| 1766 | ClangAsmParserCallback(Parser &P, SourceLocation Loc, |
| 1767 | StringRef AsmString, |
| 1768 | ArrayRef<Token> Toks, |
| 1769 | ArrayRef<unsigned> Offsets) |
| 1770 | : TheParser(P), AsmLoc(Loc), AsmString(AsmString), |
| 1771 | AsmToks(Toks), AsmTokOffsets(Offsets) { |
| 1772 | assert(AsmToks.size() == AsmTokOffsets.size()); |
| 1773 | } |
| 1774 | |
| 1775 | void *LookupInlineAsmIdentifier(StringRef &LineBuf, |
| 1776 | InlineAsmIdentifierInfo &Info, |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 1777 | bool IsUnevaluatedContext) override { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1778 | // Collect the desired tokens. |
| 1779 | SmallVector<Token, 16> LineToks; |
| 1780 | const Token *FirstOrigToken = 0; |
| 1781 | findTokensForString(LineBuf, LineToks, FirstOrigToken); |
| 1782 | |
| 1783 | unsigned NumConsumedToks; |
| 1784 | ExprResult Result = |
| 1785 | TheParser.ParseMSAsmIdentifier(LineToks, NumConsumedToks, &Info, |
| 1786 | IsUnevaluatedContext); |
| 1787 | |
| 1788 | // If we consumed the entire line, tell MC that. |
| 1789 | // Also do this if we consumed nothing as a way of reporting failure. |
| 1790 | if (NumConsumedToks == 0 || NumConsumedToks == LineToks.size()) { |
| 1791 | // By not modifying LineBuf, we're implicitly consuming it all. |
| 1792 | |
| 1793 | // Otherwise, consume up to the original tokens. |
| 1794 | } else { |
| 1795 | assert(FirstOrigToken && "not using original tokens?"); |
| 1796 | |
| 1797 | // Since we're using original tokens, apply that offset. |
| 1798 | assert(FirstOrigToken[NumConsumedToks].getLocation() |
| 1799 | == LineToks[NumConsumedToks].getLocation()); |
| 1800 | unsigned FirstIndex = FirstOrigToken - AsmToks.begin(); |
| 1801 | unsigned LastIndex = FirstIndex + NumConsumedToks - 1; |
| 1802 | |
| 1803 | // The total length we've consumed is the relative offset |
| 1804 | // of the last token we consumed plus its length. |
| 1805 | unsigned TotalOffset = (AsmTokOffsets[LastIndex] |
| 1806 | + AsmToks[LastIndex].getLength() |
| 1807 | - AsmTokOffsets[FirstIndex]); |
| 1808 | LineBuf = LineBuf.substr(0, TotalOffset); |
| 1809 | } |
| 1810 | |
| 1811 | // Initialize the "decl" with the lookup result. |
| 1812 | Info.OpDecl = static_cast<void*>(Result.take()); |
| 1813 | return Info.OpDecl; |
| 1814 | } |
| 1815 | |
| 1816 | bool LookupInlineAsmField(StringRef Base, StringRef Member, |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 1817 | unsigned &Offset) override { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1818 | return TheParser.getActions().LookupInlineAsmField(Base, Member, |
| 1819 | Offset, AsmLoc); |
| 1820 | } |
| 1821 | |
| 1822 | static void DiagHandlerCallback(const llvm::SMDiagnostic &D, |
| 1823 | void *Context) { |
| 1824 | ((ClangAsmParserCallback*) Context)->handleDiagnostic(D); |
| 1825 | } |
| 1826 | |
| 1827 | private: |
| 1828 | /// Collect the appropriate tokens for the given string. |
| 1829 | void findTokensForString(StringRef Str, SmallVectorImpl<Token> &TempToks, |
| 1830 | const Token *&FirstOrigToken) const { |
| 1831 | // For now, assert that the string we're working with is a substring |
| 1832 | // of what we gave to MC. This lets us use the original tokens. |
| 1833 | assert(!std::less<const char*>()(Str.begin(), AsmString.begin()) && |
| 1834 | !std::less<const char*>()(AsmString.end(), Str.end())); |
| 1835 | |
| 1836 | // Try to find a token whose offset matches the first token. |
| 1837 | unsigned FirstCharOffset = Str.begin() - AsmString.begin(); |
| 1838 | const unsigned *FirstTokOffset |
| 1839 | = std::lower_bound(AsmTokOffsets.begin(), AsmTokOffsets.end(), |
| 1840 | FirstCharOffset); |
| 1841 | |
| 1842 | // For now, assert that the start of the string exactly |
| 1843 | // corresponds to the start of a token. |
| 1844 | assert(*FirstTokOffset == FirstCharOffset); |
| 1845 | |
| 1846 | // Use all the original tokens for this line. (We assume the |
| 1847 | // end of the line corresponds cleanly to a token break.) |
| 1848 | unsigned FirstTokIndex = FirstTokOffset - AsmTokOffsets.begin(); |
| 1849 | FirstOrigToken = &AsmToks[FirstTokIndex]; |
| 1850 | unsigned LastCharOffset = Str.end() - AsmString.begin(); |
| 1851 | for (unsigned i = FirstTokIndex, e = AsmTokOffsets.size(); i != e; ++i) { |
| 1852 | if (AsmTokOffsets[i] >= LastCharOffset) break; |
| 1853 | TempToks.push_back(AsmToks[i]); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | void handleDiagnostic(const llvm::SMDiagnostic &D) { |
| 1858 | // Compute an offset into the inline asm buffer. |
| 1859 | // FIXME: This isn't right if .macro is involved (but hopefully, no |
| 1860 | // real-world code does that). |
| 1861 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
| 1862 | const llvm::MemoryBuffer *LBuf = |
| 1863 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
| 1864 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
| 1865 | |
| 1866 | // Figure out which token that offset points into. |
| 1867 | const unsigned *TokOffsetPtr = |
| 1868 | std::lower_bound(AsmTokOffsets.begin(), AsmTokOffsets.end(), Offset); |
| 1869 | unsigned TokIndex = TokOffsetPtr - AsmTokOffsets.begin(); |
| 1870 | unsigned TokOffset = *TokOffsetPtr; |
| 1871 | |
| 1872 | // If we come up with an answer which seems sane, use it; otherwise, |
| 1873 | // just point at the __asm keyword. |
| 1874 | // FIXME: Assert the answer is sane once we handle .macro correctly. |
| 1875 | SourceLocation Loc = AsmLoc; |
| 1876 | if (TokIndex < AsmToks.size()) { |
| 1877 | const Token &Tok = AsmToks[TokIndex]; |
| 1878 | Loc = Tok.getLocation(); |
| 1879 | Loc = Loc.getLocWithOffset(Offset - TokOffset); |
| 1880 | } |
| 1881 | TheParser.Diag(Loc, diag::err_inline_ms_asm_parsing) |
| 1882 | << D.getMessage(); |
| 1883 | } |
| 1884 | }; |
| 1885 | } |
| 1886 | |
| 1887 | /// Parse an identifier in an MS-style inline assembly block. |
| 1888 | /// |
| 1889 | /// \param CastInfo - a void* so that we don't have to teach Parser.h |
| 1890 | /// about the actual type. |
| 1891 | ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> &LineToks, |
| 1892 | unsigned &NumLineToksConsumed, |
| 1893 | void *CastInfo, |
| 1894 | bool IsUnevaluatedContext) { |
| 1895 | llvm::InlineAsmIdentifierInfo &Info = |
| 1896 | *(llvm::InlineAsmIdentifierInfo *) CastInfo; |
| 1897 | |
| 1898 | // Push a fake token on the end so that we don't overrun the token |
| 1899 | // stream. We use ';' because it expression-parsing should never |
| 1900 | // overrun it. |
| 1901 | const tok::TokenKind EndOfStream = tok::semi; |
| 1902 | Token EndOfStreamTok; |
| 1903 | EndOfStreamTok.startToken(); |
| 1904 | EndOfStreamTok.setKind(EndOfStream); |
| 1905 | LineToks.push_back(EndOfStreamTok); |
| 1906 | |
| 1907 | // Also copy the current token over. |
| 1908 | LineToks.push_back(Tok); |
| 1909 | |
| 1910 | PP.EnterTokenStream(LineToks.begin(), |
| 1911 | LineToks.size(), |
| 1912 | /*disable macros*/ true, |
| 1913 | /*owns tokens*/ false); |
| 1914 | |
| 1915 | // Clear the current token and advance to the first token in LineToks. |
| 1916 | ConsumeAnyToken(); |
| 1917 | |
| 1918 | // Parse an optional scope-specifier if we're in C++. |
| 1919 | CXXScopeSpec SS; |
| 1920 | if (getLangOpts().CPlusPlus) { |
| 1921 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
| 1922 | } |
| 1923 | |
| 1924 | // Require an identifier here. |
| 1925 | SourceLocation TemplateKWLoc; |
| 1926 | UnqualifiedId Id; |
| 1927 | bool Invalid = ParseUnqualifiedId(SS, |
| 1928 | /*EnteringContext=*/false, |
| 1929 | /*AllowDestructorName=*/false, |
| 1930 | /*AllowConstructorName=*/false, |
| 1931 | /*ObjectType=*/ ParsedType(), |
| 1932 | TemplateKWLoc, |
| 1933 | Id); |
| 1934 | |
Dmitri Gribenko | fc13b8b | 2013-12-03 00:48:09 +0000 | [diff] [blame] | 1935 | // Figure out how many tokens we are into LineToks. |
| 1936 | unsigned LineIndex = 0; |
| 1937 | if (Tok.is(EndOfStream)) { |
| 1938 | LineIndex = LineToks.size() - 2; |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1939 | } else { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1940 | while (LineToks[LineIndex].getLocation() != Tok.getLocation()) { |
| 1941 | LineIndex++; |
| 1942 | assert(LineIndex < LineToks.size() - 2); // we added two extra tokens |
| 1943 | } |
Dmitri Gribenko | fc13b8b | 2013-12-03 00:48:09 +0000 | [diff] [blame] | 1944 | } |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1945 | |
Dmitri Gribenko | fc13b8b | 2013-12-03 00:48:09 +0000 | [diff] [blame] | 1946 | // If we've run into the poison token we inserted before, or there |
| 1947 | // was a parsing error, then claim the entire line. |
| 1948 | if (Invalid || Tok.is(EndOfStream)) { |
| 1949 | NumLineToksConsumed = LineToks.size() - 2; |
| 1950 | } else { |
| 1951 | // Otherwise, claim up to the start of the next token. |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1952 | NumLineToksConsumed = LineIndex; |
| 1953 | } |
Dmitri Gribenko | fc13b8b | 2013-12-03 00:48:09 +0000 | [diff] [blame] | 1954 | |
| 1955 | // Finally, restore the old parsing state by consuming all the tokens we |
| 1956 | // staged before, implicitly killing off the token-lexer we pushed. |
| 1957 | for (unsigned i = 0, e = LineToks.size() - LineIndex - 2; i != e; ++i) { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1958 | ConsumeAnyToken(); |
| 1959 | } |
Dmitri Gribenko | fc13b8b | 2013-12-03 00:48:09 +0000 | [diff] [blame] | 1960 | assert(Tok.is(EndOfStream)); |
| 1961 | ConsumeToken(); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Leave LineToks in its original state. |
| 1964 | LineToks.pop_back(); |
| 1965 | LineToks.pop_back(); |
| 1966 | |
| 1967 | // Perform the lookup. |
| 1968 | return Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id, Info, |
| 1969 | IsUnevaluatedContext); |
| 1970 | } |
| 1971 | |
| 1972 | /// Turn a sequence of our tokens back into a string that we can hand |
| 1973 | /// to the MC asm parser. |
| 1974 | static bool buildMSAsmString(Preprocessor &PP, |
| 1975 | SourceLocation AsmLoc, |
| 1976 | ArrayRef<Token> AsmToks, |
| 1977 | SmallVectorImpl<unsigned> &TokOffsets, |
| 1978 | SmallString<512> &Asm) { |
| 1979 | assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!"); |
| 1980 | |
| 1981 | // Is this the start of a new assembly statement? |
| 1982 | bool isNewStatement = true; |
| 1983 | |
| 1984 | for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) { |
| 1985 | const Token &Tok = AsmToks[i]; |
| 1986 | |
| 1987 | // Start each new statement with a newline and a tab. |
| 1988 | if (!isNewStatement && |
| 1989 | (Tok.is(tok::kw_asm) || Tok.isAtStartOfLine())) { |
| 1990 | Asm += "\n\t"; |
| 1991 | isNewStatement = true; |
| 1992 | } |
| 1993 | |
| 1994 | // Preserve the existence of leading whitespace except at the |
| 1995 | // start of a statement. |
| 1996 | if (!isNewStatement && Tok.hasLeadingSpace()) |
| 1997 | Asm += ' '; |
| 1998 | |
| 1999 | // Remember the offset of this token. |
| 2000 | TokOffsets.push_back(Asm.size()); |
| 2001 | |
| 2002 | // Don't actually write '__asm' into the assembly stream. |
| 2003 | if (Tok.is(tok::kw_asm)) { |
| 2004 | // Complain about __asm at the end of the stream. |
| 2005 | if (i + 1 == e) { |
| 2006 | PP.Diag(AsmLoc, diag::err_asm_empty); |
| 2007 | return true; |
| 2008 | } |
| 2009 | |
| 2010 | continue; |
| 2011 | } |
| 2012 | |
| 2013 | // Append the spelling of the token. |
| 2014 | SmallString<32> SpellingBuffer; |
| 2015 | bool SpellingInvalid = false; |
| 2016 | Asm += PP.getSpelling(Tok, SpellingBuffer, &SpellingInvalid); |
| 2017 | assert(!SpellingInvalid && "spelling was invalid after correct parse?"); |
| 2018 | |
| 2019 | // We are no longer at the start of a statement. |
| 2020 | isNewStatement = false; |
| 2021 | } |
| 2022 | |
| 2023 | // Ensure that the buffer is null-terminated. |
| 2024 | Asm.push_back('\0'); |
| 2025 | Asm.pop_back(); |
| 2026 | |
| 2027 | assert(TokOffsets.size() == AsmToks.size()); |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
Eli Friedman | a4b02c3 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 2031 | /// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled, |
| 2032 | /// this routine is called to collect the tokens for an MS asm statement. |
Chad Rosier | 3250302 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 2033 | /// |
| 2034 | /// [MS] ms-asm-statement: |
| 2035 | /// ms-asm-block |
| 2036 | /// ms-asm-block ms-asm-statement |
| 2037 | /// |
| 2038 | /// [MS] ms-asm-block: |
| 2039 | /// '__asm' ms-asm-line '\n' |
| 2040 | /// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt] |
| 2041 | /// |
| 2042 | /// [MS] ms-asm-instruction-block |
| 2043 | /// ms-asm-line |
| 2044 | /// ms-asm-line '\n' ms-asm-instruction-block |
| 2045 | /// |
Eli Friedman | a4b02c3 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 2046 | StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) { |
| 2047 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 2048 | SourceLocation EndLoc = AsmLoc; |
Chad Rosier | 3250302 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 2049 | SmallVector<Token, 4> AsmToks; |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2050 | |
| 2051 | bool InBraces = false; |
| 2052 | unsigned short savedBraceCount = 0; |
| 2053 | bool InAsmComment = false; |
| 2054 | FileID FID; |
| 2055 | unsigned LineNo = 0; |
| 2056 | unsigned NumTokensRead = 0; |
| 2057 | SourceLocation LBraceLoc; |
| 2058 | |
| 2059 | if (Tok.is(tok::l_brace)) { |
| 2060 | // Braced inline asm: consume the opening brace. |
| 2061 | InBraces = true; |
| 2062 | savedBraceCount = BraceCount; |
| 2063 | EndLoc = LBraceLoc = ConsumeBrace(); |
| 2064 | ++NumTokensRead; |
| 2065 | } else { |
| 2066 | // Single-line inline asm; compute which line it is on. |
| 2067 | std::pair<FileID, unsigned> ExpAsmLoc = |
| 2068 | SrcMgr.getDecomposedExpansionLoc(EndLoc); |
| 2069 | FID = ExpAsmLoc.first; |
| 2070 | LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second); |
| 2071 | } |
| 2072 | |
| 2073 | SourceLocation TokLoc = Tok.getLocation(); |
Eli Friedman | a4b02c3 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 2074 | do { |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2075 | // If we hit EOF, we're done, period. |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 2076 | if (isEofOrEom()) |
Eli Friedman | a4b02c3 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 2077 | break; |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2078 | |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2079 | if (!InAsmComment && Tok.is(tok::semi)) { |
| 2080 | // A semicolon in an asm is the start of a comment. |
| 2081 | InAsmComment = true; |
| 2082 | if (InBraces) { |
| 2083 | // Compute which line the comment is on. |
| 2084 | std::pair<FileID, unsigned> ExpSemiLoc = |
| 2085 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
| 2086 | FID = ExpSemiLoc.first; |
| 2087 | LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second); |
| 2088 | } |
| 2089 | } else if (!InBraces || InAsmComment) { |
| 2090 | // If end-of-line is significant, check whether this token is on a |
| 2091 | // new line. |
| 2092 | std::pair<FileID, unsigned> ExpLoc = |
| 2093 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
| 2094 | if (ExpLoc.first != FID || |
| 2095 | SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) { |
| 2096 | // If this is a single-line __asm, we're done. |
| 2097 | if (!InBraces) |
| 2098 | break; |
| 2099 | // We're no longer in a comment. |
| 2100 | InAsmComment = false; |
| 2101 | } else if (!InAsmComment && Tok.is(tok::r_brace)) { |
| 2102 | // Single-line asm always ends when a closing brace is seen. |
| 2103 | // FIXME: This is compatible with Apple gcc's -fasm-blocks; what |
| 2104 | // does MSVC do here? |
| 2105 | break; |
| 2106 | } |
| 2107 | } |
| 2108 | if (!InAsmComment && InBraces && Tok.is(tok::r_brace) && |
| 2109 | BraceCount == (savedBraceCount + 1)) { |
| 2110 | // Consume the closing brace, and finish |
| 2111 | EndLoc = ConsumeBrace(); |
| 2112 | break; |
| 2113 | } |
| 2114 | |
| 2115 | // Consume the next token; make sure we don't modify the brace count etc. |
| 2116 | // if we are in a comment. |
| 2117 | EndLoc = TokLoc; |
| 2118 | if (InAsmComment) |
| 2119 | PP.Lex(Tok); |
| 2120 | else { |
| 2121 | AsmToks.push_back(Tok); |
| 2122 | ConsumeAnyToken(); |
| 2123 | } |
| 2124 | TokLoc = Tok.getLocation(); |
| 2125 | ++NumTokensRead; |
Eli Friedman | a4b02c3 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 2126 | } while (1); |
Chad Rosier | 3250302 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 2127 | |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2128 | if (InBraces && BraceCount != savedBraceCount) { |
| 2129 | // __asm without closing brace (this can happen at EOF). |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2130 | Diag(Tok, diag::err_expected) << tok::r_brace; |
| 2131 | Diag(LBraceLoc, diag::note_matching) << tok::l_brace; |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2132 | return StmtError(); |
| 2133 | } else if (NumTokensRead == 0) { |
| 2134 | // Empty __asm. |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2135 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Chad Rosier | c97a6bb | 2012-08-14 19:22:06 +0000 | [diff] [blame] | 2136 | return StmtError(); |
| 2137 | } |
| 2138 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2139 | // Okay, prepare to use MC to parse the assembly. |
| 2140 | SmallVector<StringRef, 4> ConstraintRefs; |
| 2141 | SmallVector<Expr*, 4> Exprs; |
| 2142 | SmallVector<StringRef, 4> ClobberRefs; |
| 2143 | |
| 2144 | // We need an actual supported target. |
Benjamin Kramer | 9299637dc | 2014-03-04 19:31:42 +0000 | [diff] [blame] | 2145 | const llvm::Triple &TheTriple = Actions.Context.getTargetInfo().getTriple(); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2146 | llvm::Triple::ArchType ArchTy = TheTriple.getArch(); |
Alp Toker | b364428 | 2013-10-30 15:07:10 +0000 | [diff] [blame] | 2147 | const std::string &TT = TheTriple.getTriple(); |
| 2148 | const llvm::Target *TheTarget = 0; |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2149 | bool UnsupportedArch = (ArchTy != llvm::Triple::x86 && |
| 2150 | ArchTy != llvm::Triple::x86_64); |
Alp Toker | b364428 | 2013-10-30 15:07:10 +0000 | [diff] [blame] | 2151 | if (UnsupportedArch) { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2152 | Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName(); |
Alp Toker | b364428 | 2013-10-30 15:07:10 +0000 | [diff] [blame] | 2153 | } else { |
| 2154 | std::string Error; |
| 2155 | TheTarget = llvm::TargetRegistry::lookupTarget(TT, Error); |
| 2156 | if (!TheTarget) |
| 2157 | Diag(AsmLoc, diag::err_msasm_unable_to_create_target) << Error; |
| 2158 | } |
Alp Toker | 45cf31f | 2013-10-30 14:29:28 +0000 | [diff] [blame] | 2159 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2160 | // If we don't support assembly, or the assembly is empty, we don't |
| 2161 | // need to instantiate the AsmParser, etc. |
Alp Toker | b364428 | 2013-10-30 15:07:10 +0000 | [diff] [blame] | 2162 | if (!TheTarget || AsmToks.empty()) { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2163 | return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, StringRef(), |
| 2164 | /*NumOutputs*/ 0, /*NumInputs*/ 0, |
| 2165 | ConstraintRefs, ClobberRefs, Exprs, EndLoc); |
| 2166 | } |
| 2167 | |
| 2168 | // Expand the tokens into a string buffer. |
| 2169 | SmallString<512> AsmString; |
| 2170 | SmallVector<unsigned, 8> TokOffsets; |
| 2171 | if (buildMSAsmString(PP, AsmLoc, AsmToks, TokOffsets, AsmString)) |
| 2172 | return StmtError(); |
| 2173 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 2174 | std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT)); |
| 2175 | std::unique_ptr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TT)); |
Joey Gouly | 92dfcfa | 2013-09-12 10:59:24 +0000 | [diff] [blame] | 2176 | // Get the instruction descriptor. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 2177 | const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo(); |
| 2178 | std::unique_ptr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo()); |
| 2179 | std::unique_ptr<llvm::MCSubtargetInfo> STI( |
| 2180 | TheTarget->createMCSubtargetInfo(TT, "", "")); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2181 | |
| 2182 | llvm::SourceMgr TempSrcMgr; |
Bill Wendling | da1e3e7 | 2013-06-18 07:22:05 +0000 | [diff] [blame] | 2183 | llvm::MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &TempSrcMgr); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2184 | llvm::MemoryBuffer *Buffer = |
| 2185 | llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>"); |
| 2186 | |
| 2187 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 2188 | TempSrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc()); |
| 2189 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 2190 | std::unique_ptr<llvm::MCStreamer> Str(createNullStreamer(Ctx)); |
| 2191 | std::unique_ptr<llvm::MCAsmParser> Parser( |
| 2192 | createMCAsmParser(TempSrcMgr, Ctx, *Str.get(), *MAI)); |
| 2193 | std::unique_ptr<llvm::MCTargetAsmParser> TargetParser( |
| 2194 | TheTarget->createMCAsmParser(*STI, *Parser, *MII)); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2195 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2196 | llvm::MCInstPrinter *IP = |
| 2197 | TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI); |
| 2198 | |
| 2199 | // Change to the Intel dialect. |
| 2200 | Parser->setAssemblerDialect(1); |
| 2201 | Parser->setTargetParser(*TargetParser.get()); |
| 2202 | Parser->setParsingInlineAsm(true); |
| 2203 | TargetParser->setParsingInlineAsm(true); |
| 2204 | |
| 2205 | ClangAsmParserCallback Callback(*this, AsmLoc, AsmString, |
| 2206 | AsmToks, TokOffsets); |
| 2207 | TargetParser->setSemaCallback(&Callback); |
| 2208 | TempSrcMgr.setDiagHandler(ClangAsmParserCallback::DiagHandlerCallback, |
| 2209 | &Callback); |
| 2210 | |
| 2211 | unsigned NumOutputs; |
| 2212 | unsigned NumInputs; |
| 2213 | std::string AsmStringIR; |
| 2214 | SmallVector<std::pair<void *, bool>, 4> OpExprs; |
| 2215 | SmallVector<std::string, 4> Constraints; |
| 2216 | SmallVector<std::string, 4> Clobbers; |
| 2217 | if (Parser->parseMSInlineAsm(AsmLoc.getPtrEncoding(), AsmStringIR, |
| 2218 | NumOutputs, NumInputs, OpExprs, Constraints, |
| 2219 | Clobbers, MII, IP, Callback)) |
| 2220 | return StmtError(); |
| 2221 | |
Reid Kleckner | 185940a | 2014-03-27 00:00:03 +0000 | [diff] [blame] | 2222 | // Filter out "fpsw". Clang doesn't accept it, and it always lists flags and |
| 2223 | // fpsr as clobbers. |
| 2224 | auto End = std::remove(Clobbers.begin(), Clobbers.end(), "fpsw"); |
| 2225 | Clobbers.erase(End, Clobbers.end()); |
| 2226 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2227 | // Build the vector of clobber StringRefs. |
| 2228 | unsigned NumClobbers = Clobbers.size(); |
| 2229 | ClobberRefs.resize(NumClobbers); |
| 2230 | for (unsigned i = 0; i != NumClobbers; ++i) |
| 2231 | ClobberRefs[i] = StringRef(Clobbers[i]); |
| 2232 | |
| 2233 | // Recast the void pointers and build the vector of constraint StringRefs. |
| 2234 | unsigned NumExprs = NumOutputs + NumInputs; |
| 2235 | ConstraintRefs.resize(NumExprs); |
| 2236 | Exprs.resize(NumExprs); |
| 2237 | for (unsigned i = 0, e = NumExprs; i != e; ++i) { |
| 2238 | Expr *OpExpr = static_cast<Expr *>(OpExprs[i].first); |
| 2239 | if (!OpExpr) |
| 2240 | return StmtError(); |
| 2241 | |
| 2242 | // Need address of variable. |
| 2243 | if (OpExprs[i].second) |
| 2244 | OpExpr = Actions.BuildUnaryOp(getCurScope(), AsmLoc, UO_AddrOf, OpExpr) |
| 2245 | .take(); |
| 2246 | |
| 2247 | ConstraintRefs[i] = StringRef(Constraints[i]); |
| 2248 | Exprs[i] = OpExpr; |
| 2249 | } |
| 2250 | |
Chad Rosier | c6c7133 | 2012-08-06 20:03:45 +0000 | [diff] [blame] | 2251 | // FIXME: We should be passing source locations for better diagnostics. |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2252 | return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, AsmStringIR, |
| 2253 | NumOutputs, NumInputs, |
| 2254 | ConstraintRefs, ClobberRefs, Exprs, EndLoc); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2257 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2258 | /// asm-statement: |
| 2259 | /// gnu-asm-statement |
| 2260 | /// ms-asm-statement |
| 2261 | /// |
| 2262 | /// [GNU] gnu-asm-statement: |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2263 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 2264 | /// |
| 2265 | /// [GNU] asm-argument: |
| 2266 | /// asm-string-literal |
| 2267 | /// asm-string-literal ':' asm-operands[opt] |
| 2268 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 2269 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 2270 | /// ':' asm-clobbers |
| 2271 | /// |
| 2272 | /// [GNU] asm-clobbers: |
| 2273 | /// asm-string-literal |
| 2274 | /// asm-clobbers ',' asm-string-literal |
| 2275 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2276 | StmtResult Parser::ParseAsmStatement(bool &msAsm) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 2277 | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
Chris Lattner | 73c56c0 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 2278 | SourceLocation AsmLoc = ConsumeToken(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 2279 | |
Chad Rosier | c8e56e8 | 2012-12-05 21:08:21 +0000 | [diff] [blame] | 2280 | if (getLangOpts().AsmBlocks && Tok.isNot(tok::l_paren) && |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2281 | !isTypeQualifier()) { |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 2282 | msAsm = true; |
Eli Friedman | a4b02c3 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 2283 | return ParseMicrosoftAsmStatement(AsmLoc); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 2284 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2285 | DeclSpec DS(AttrFactory); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2286 | SourceLocation Loc = Tok.getLocation(); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2287 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 2288 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2289 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2290 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2291 | Diag(Loc, diag::w_asm_qualifier_ignored) << "const"; |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2292 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2293 | Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 2294 | // FIXME: Once GCC supports _Atomic, check whether it permits it here. |
| 2295 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 2296 | Diag(Loc, diag::w_asm_qualifier_ignored) << "_Atomic"; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2298 | // Remember if this was a volatile asm. |
Anders Carlsson | 660bdd1 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 2299 | bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 2300 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2301 | Diag(Tok, diag::err_expected_lparen_after) << "asm"; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2302 | SkipUntil(tok::r_paren, StopAtSemi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 2303 | return StmtError(); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2304 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2305 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2306 | T.consumeOpen(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 2307 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2308 | ExprResult AsmString(ParseAsmStringLiteral()); |
Ted Kremenek | eb0a6c0 | 2011-12-02 01:30:14 +0000 | [diff] [blame] | 2309 | if (AsmString.isInvalid()) { |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 2310 | // Consume up to and including the closing paren. |
| 2311 | T.skipToEnd(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 2312 | return StmtError(); |
Ted Kremenek | eb0a6c0 | 2011-12-02 01:30:14 +0000 | [diff] [blame] | 2313 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2314 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2315 | SmallVector<IdentifierInfo *, 4> Names; |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2316 | ExprVector Constraints; |
| 2317 | ExprVector Exprs; |
| 2318 | ExprVector Clobbers; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2319 | |
Anders Carlsson | 19fe116 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 2320 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2321 | // We have a simple asm expression like 'asm("foo")'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2322 | T.consumeClose(); |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 2323 | return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile, |
| 2324 | /*NumOutputs*/ 0, /*NumInputs*/ 0, 0, |
| 2325 | Constraints, Exprs, AsmString.take(), |
| 2326 | Clobbers, T.getCloseLocation()); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2327 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2328 | |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2329 | // Parse Outputs, if present. |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 2330 | bool AteExtraColon = false; |
| 2331 | if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 2332 | // In C++ mode, parse "::" like ": :". |
| 2333 | AteExtraColon = Tok.is(tok::coloncolon); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2334 | ConsumeToken(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 2336 | if (!AteExtraColon && |
| 2337 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2338 | return StmtError(); |
| 2339 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2340 | |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2341 | unsigned NumOutputs = Names.size(); |
| 2342 | |
| 2343 | // Parse Inputs, if present. |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 2344 | if (AteExtraColon || |
| 2345 | Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 2346 | // In C++ mode, parse "::" like ": :". |
| 2347 | if (AteExtraColon) |
| 2348 | AteExtraColon = false; |
| 2349 | else { |
| 2350 | AteExtraColon = Tok.is(tok::coloncolon); |
| 2351 | ConsumeToken(); |
| 2352 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2353 | |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 2354 | if (!AteExtraColon && |
| 2355 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2356 | return StmtError(); |
| 2357 | } |
| 2358 | |
| 2359 | assert(Names.size() == Constraints.size() && |
| 2360 | Constraints.size() == Exprs.size() && |
| 2361 | "Input operand size mismatch!"); |
| 2362 | |
| 2363 | unsigned NumInputs = Names.size() - NumOutputs; |
| 2364 | |
| 2365 | // Parse the clobbers, if present. |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 2366 | if (AteExtraColon || Tok.is(tok::colon)) { |
| 2367 | if (!AteExtraColon) |
| 2368 | ConsumeToken(); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2369 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 2370 | // Parse the asm-string list for clobbers if present. |
| 2371 | if (Tok.isNot(tok::r_paren)) { |
| 2372 | while (1) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2373 | ExprResult Clobber(ParseAsmStringLiteral()); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2374 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 2375 | if (Clobber.isInvalid()) |
| 2376 | break; |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2377 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 2378 | Clobbers.push_back(Clobber.release()); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2379 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2380 | if (!TryConsumeToken(tok::comma)) |
| 2381 | break; |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 2382 | } |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2386 | T.consumeClose(); |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 2387 | return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs, |
| 2388 | NumInputs, Names.data(), Constraints, Exprs, |
| 2389 | AsmString.take(), Clobbers, |
| 2390 | T.getCloseLocation()); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | /// ParseAsmOperands - Parse the asm-operands production as used by |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 2394 | /// asm-statement, assuming the leading ':' token was eaten. |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2395 | /// |
| 2396 | /// [GNU] asm-operands: |
| 2397 | /// asm-operand |
| 2398 | /// asm-operands ',' asm-operand |
| 2399 | /// |
| 2400 | /// [GNU] asm-operand: |
| 2401 | /// asm-string-literal '(' expression ')' |
| 2402 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 2403 | /// |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 2404 | // |
| 2405 | // FIXME: Avoid unnecessary std::string trashing. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2406 | bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names, |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 2407 | SmallVectorImpl<Expr *> &Constraints, |
| 2408 | SmallVectorImpl<Expr *> &Exprs) { |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2409 | // 'asm-operands' isn't present? |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 2410 | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 2411 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
| 2413 | while (1) { |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2414 | // Read the [id] if present. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 2415 | if (Tok.is(tok::l_square)) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2416 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 2417 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 2419 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2420 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2421 | SkipUntil(tok::r_paren, StopAtSemi); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 2422 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2423 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 2425 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 645ff3f | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 2426 | ConsumeToken(); |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 2427 | |
Anders Carlsson | 9a020f9 | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 2428 | Names.push_back(II); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2429 | T.consumeClose(); |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 2430 | } else |
Anders Carlsson | 9a020f9 | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 2431 | Names.push_back(0); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2432 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2433 | ExprResult Constraint(ParseAsmStringLiteral()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2434 | if (Constraint.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2435 | SkipUntil(tok::r_paren, StopAtSemi); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 2436 | return true; |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 2437 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2438 | Constraints.push_back(Constraint.release()); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2439 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 2440 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2441 | Diag(Tok, diag::err_expected_lparen_after) << "asm operand"; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2442 | SkipUntil(tok::r_paren, StopAtSemi); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 2443 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2444 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2446 | // Read the parenthesized expression. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2447 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2448 | T.consumeOpen(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2449 | ExprResult Res(ParseExpression()); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2450 | T.consumeClose(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2451 | if (Res.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2452 | SkipUntil(tok::r_paren, StopAtSemi); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 2453 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2454 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2455 | Exprs.push_back(Res.release()); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2456 | // Eat the comma and continue parsing if it exists. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2457 | if (!TryConsumeToken(tok::comma)) |
| 2458 | return false; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 2461 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2462 | Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) { |
Chris Lattner | 12f2ea5 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 2463 | assert(Tok.is(tok::l_brace)); |
| 2464 | SourceLocation LBraceLoc = Tok.getLocation(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2465 | |
Argyrios Kyrtzidis | 4431918 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 2466 | if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) && |
Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 2467 | trySkippingFunctionBody()) { |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2468 | BodyScope.Exit(); |
Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 2469 | return Actions.ActOnSkippedFunctionBody(Decl); |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2470 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2471 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2472 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc, |
| 2473 | "parsing function body"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 2475 | // Do not enter a scope for the brace, as the arguments are in the same scope |
| 2476 | // (the function body) as the body itself. Instead, just read the statement |
| 2477 | // list and put it into a CompoundStmt for safe keeping. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2478 | StmtResult FnBody(ParseCompoundStatementBody()); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 2479 | |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 2480 | // If the function body could not be parsed, make a bogus compoundstmt. |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2481 | if (FnBody.isInvalid()) { |
| 2482 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 2483 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2484 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2486 | BodyScope.Exit(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2487 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Seo Sanghyeon | 34f92ac | 2007-12-01 08:06:07 +0000 | [diff] [blame] | 2488 | } |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2489 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2490 | /// ParseFunctionTryBlock - Parse a C++ function-try-block. |
| 2491 | /// |
| 2492 | /// function-try-block: |
| 2493 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 2494 | /// |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2495 | Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) { |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2496 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 2497 | SourceLocation TryLoc = ConsumeToken(); |
| 2498 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2499 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc, |
| 2500 | "parsing function try block"); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2501 | |
| 2502 | // Constructor initializer list? |
| 2503 | if (Tok.is(tok::colon)) |
| 2504 | ParseConstructorInitializer(Decl); |
Douglas Gregor | 5ca153f | 2011-09-07 20:36:12 +0000 | [diff] [blame] | 2505 | else |
| 2506 | Actions.ActOnDefaultCtorInitializers(Decl); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2507 | |
Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 2508 | if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) && |
| 2509 | trySkippingFunctionBody()) { |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2510 | BodyScope.Exit(); |
Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 2511 | return Actions.ActOnSkippedFunctionBody(Decl); |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2512 | } |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2513 | |
Sebastian Redl | d98ecd6 | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 2514 | SourceLocation LBraceLoc = Tok.getLocation(); |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2515 | StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true)); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2516 | // If we failed to parse the try-catch, we just give the function an empty |
| 2517 | // compound statement as the body. |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2518 | if (FnBody.isInvalid()) { |
| 2519 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 2520 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2521 | } |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2522 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2523 | BodyScope.Exit(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2524 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2527 | bool Parser::trySkippingFunctionBody() { |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2528 | assert(Tok.is(tok::l_brace)); |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2529 | assert(SkipFunctionBodies && |
| 2530 | "Should only be called when SkipFunctionBodies is enabled"); |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2531 | |
Argyrios Kyrtzidis | 289e4a3 | 2012-10-31 17:29:28 +0000 | [diff] [blame] | 2532 | if (!PP.isCodeCompletionEnabled()) { |
| 2533 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2534 | SkipUntil(tok::r_brace); |
Argyrios Kyrtzidis | 289e4a3 | 2012-10-31 17:29:28 +0000 | [diff] [blame] | 2535 | return true; |
| 2536 | } |
| 2537 | |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2538 | // We're in code-completion mode. Skip parsing for all function bodies unless |
| 2539 | // the body contains the code-completion point. |
| 2540 | TentativeParsingAction PA(*this); |
| 2541 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2542 | if (SkipUntil(tok::r_brace, StopAtCodeCompletion)) { |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2543 | PA.Commit(); |
| 2544 | return true; |
| 2545 | } |
| 2546 | |
| 2547 | PA.Revert(); |
| 2548 | return false; |
| 2549 | } |
| 2550 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2551 | /// ParseCXXTryBlock - Parse a C++ try-block. |
| 2552 | /// |
| 2553 | /// try-block: |
| 2554 | /// 'try' compound-statement handler-seq |
| 2555 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2556 | StmtResult Parser::ParseCXXTryBlock() { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2557 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 2558 | |
| 2559 | SourceLocation TryLoc = ConsumeToken(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2560 | return ParseCXXTryBlockCommon(TryLoc); |
| 2561 | } |
| 2562 | |
| 2563 | /// ParseCXXTryBlockCommon - Parse the common part of try-block and |
| 2564 | /// function-try-block. |
| 2565 | /// |
| 2566 | /// try-block: |
| 2567 | /// 'try' compound-statement handler-seq |
| 2568 | /// |
| 2569 | /// function-try-block: |
| 2570 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 2571 | /// |
| 2572 | /// handler-seq: |
| 2573 | /// handler handler-seq[opt] |
| 2574 | /// |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2575 | /// [Borland] try-block: |
| 2576 | /// 'try' compound-statement seh-except-block |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 2577 | /// 'try' compound-statement seh-finally-block |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2578 | /// |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2579 | StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2580 | if (Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2581 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2582 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2583 | |
| 2584 | StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false, |
David Blaikie | 3403feb | 2012-11-13 18:51:45 +0000 | [diff] [blame] | 2585 | Scope::DeclScope | Scope::TryScope | |
| 2586 | (FnTry ? Scope::FnTryCatchScope : 0))); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2587 | if (TryBlock.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2588 | return TryBlock; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2589 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2590 | // Borland allows SEH-handlers with 'try' |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2591 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2592 | if ((Tok.is(tok::identifier) && |
| 2593 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) || |
| 2594 | Tok.is(tok::kw___finally)) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2595 | // TODO: Factor into common return ParseSEHHandlerCommon(...) |
| 2596 | StmtResult Handler; |
Douglas Gregor | 60060d6 | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 2597 | if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2598 | SourceLocation Loc = ConsumeToken(); |
| 2599 | Handler = ParseSEHExceptBlock(Loc); |
| 2600 | } |
| 2601 | else { |
| 2602 | SourceLocation Loc = ConsumeToken(); |
| 2603 | Handler = ParseSEHFinallyBlock(Loc); |
| 2604 | } |
| 2605 | if(Handler.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2606 | return Handler; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2607 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2608 | return Actions.ActOnSEHTryBlock(true /* IsCXXTry */, |
| 2609 | TryLoc, |
| 2610 | TryBlock.take(), |
| 2611 | Handler.take()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2612 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2613 | else { |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2614 | StmtVector Handlers; |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 2615 | |
| 2616 | // C++11 attributes can't appear here, despite this context seeming |
| 2617 | // statement-like. |
| 2618 | DiagnoseAndSkipCXX11Attributes(); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2619 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2620 | if (Tok.isNot(tok::kw_catch)) |
| 2621 | return StmtError(Diag(Tok, diag::err_expected_catch)); |
| 2622 | while (Tok.is(tok::kw_catch)) { |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2623 | StmtResult Handler(ParseCXXCatchBlock(FnTry)); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2624 | if (!Handler.isInvalid()) |
| 2625 | Handlers.push_back(Handler.release()); |
| 2626 | } |
| 2627 | // Don't bother creating the full statement if we don't have any usable |
| 2628 | // handlers. |
| 2629 | if (Handlers.empty()) |
| 2630 | return StmtError(); |
| 2631 | |
Robert Wilhelm | cafda82 | 2013-08-22 09:20:03 +0000 | [diff] [blame] | 2632 | return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), Handlers); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2633 | } |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard |
| 2637 | /// |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2638 | /// handler: |
| 2639 | /// 'catch' '(' exception-declaration ')' compound-statement |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2640 | /// |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2641 | /// exception-declaration: |
| 2642 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 2643 | /// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt] |
| 2644 | /// '...' |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2645 | /// |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2646 | StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2647 | assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); |
| 2648 | |
| 2649 | SourceLocation CatchLoc = ConsumeToken(); |
| 2650 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2651 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2652 | if (T.expectAndConsume()) |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2653 | return StmtError(); |
| 2654 | |
| 2655 | // C++ 3.3.2p3: |
| 2656 | // The name in a catch exception-declaration is local to the handler and |
| 2657 | // shall not be redeclared in the outermost block of the handler. |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2658 | ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope | |
David Blaikie | 3403feb | 2012-11-13 18:51:45 +0000 | [diff] [blame] | 2659 | (FnCatch ? Scope::FnTryCatchScope : 0)); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2660 | |
| 2661 | // exception-declaration is equivalent to '...' or a parameter-declaration |
| 2662 | // without default arguments. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2663 | Decl *ExceptionDecl = 0; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2664 | if (Tok.isNot(tok::ellipsis)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2665 | ParsedAttributesWithRange Attributes(AttrFactory); |
| 2666 | MaybeParseCXX11Attributes(Attributes); |
| 2667 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2668 | DeclSpec DS(AttrFactory); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2669 | DS.takeAttributesFrom(Attributes); |
| 2670 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2671 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 2672 | return StmtError(); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2673 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2674 | Declarator ExDecl(DS, Declarator::CXXCatchContext); |
| 2675 | ParseDeclarator(ExDecl); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2676 | ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2677 | } else |
| 2678 | ConsumeToken(); |
| 2679 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2680 | T.consumeClose(); |
| 2681 | if (T.getCloseLocation().isInvalid()) |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2682 | return StmtError(); |
| 2683 | |
| 2684 | if (Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2685 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2686 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2687 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2688 | StmtResult Block(ParseCompoundStatement()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2689 | if (Block.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2690 | return Block; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2691 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2692 | return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2693 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2694 | |
| 2695 | void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) { |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2696 | IfExistsCondition Result; |
Francois Pichet | a5b3fcb | 2011-05-07 17:30:27 +0000 | [diff] [blame] | 2697 | if (ParseMicrosoftIfExistsCondition(Result)) |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2698 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2700 | // Handle dependent statements by parsing the braces as a compound statement. |
| 2701 | // This is not the same behavior as Visual C++, which don't treat this as a |
| 2702 | // compound statement, but for Clang's type checking we can't have anything |
| 2703 | // inside these braces escaping to the surrounding code. |
| 2704 | if (Result.Behavior == IEB_Dependent) { |
| 2705 | if (!Tok.is(tok::l_brace)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2706 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2707 | return; |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2708 | } |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2709 | |
| 2710 | StmtResult Compound = ParseCompoundStatement(); |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2711 | if (Compound.isInvalid()) |
| 2712 | return; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2714 | StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc, |
| 2715 | Result.IsIfExists, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2716 | Result.SS, |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2717 | Result.Name, |
| 2718 | Compound.get()); |
| 2719 | if (DepResult.isUsable()) |
| 2720 | Stmts.push_back(DepResult.get()); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2723 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2724 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 2725 | if (Braces.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2726 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2730 | switch (Result.Behavior) { |
| 2731 | case IEB_Parse: |
| 2732 | // Parse the statements below. |
| 2733 | break; |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2734 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2735 | case IEB_Dependent: |
| 2736 | llvm_unreachable("Dependent case handled above"); |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2738 | case IEB_Skip: |
| 2739 | Braces.skipToEnd(); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
| 2742 | |
| 2743 | // Condition is true, parse the statements. |
| 2744 | while (Tok.isNot(tok::r_brace)) { |
| 2745 | StmtResult R = ParseStatementOrDeclaration(Stmts, false); |
| 2746 | if (R.isUsable()) |
| 2747 | Stmts.push_back(R.release()); |
| 2748 | } |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2749 | Braces.consumeClose(); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2750 | } |