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