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