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