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