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 | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 18 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Scope.h" |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/PrettyStackTrace.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // C99 6.8: Statements and Blocks. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 30 | /// StatementOrDeclaration: |
| 31 | /// statement |
| 32 | /// declaration |
| 33 | /// |
| 34 | /// statement: |
| 35 | /// labeled-statement |
| 36 | /// compound-statement |
| 37 | /// expression-statement |
| 38 | /// selection-statement |
| 39 | /// iteration-statement |
| 40 | /// jump-statement |
Argyrios Kyrtzidis | dcdd55f | 2008-09-07 18:58:01 +0000 | [diff] [blame] | 41 | /// [C++] declaration-statement |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 42 | /// [C++] try-block |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 43 | /// [MS] seh-try-block |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 44 | /// [OBC] objc-throw-statement |
| 45 | /// [OBC] objc-try-catch-statement |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 46 | /// [OBC] objc-synchronized-statement |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | /// [GNU] asm-statement |
| 48 | /// [OMP] openmp-construct [TODO] |
| 49 | /// |
| 50 | /// labeled-statement: |
| 51 | /// identifier ':' statement |
| 52 | /// 'case' constant-expression ':' statement |
| 53 | /// 'default' ':' statement |
| 54 | /// |
| 55 | /// selection-statement: |
| 56 | /// if-statement |
| 57 | /// switch-statement |
| 58 | /// |
| 59 | /// iteration-statement: |
| 60 | /// while-statement |
| 61 | /// do-statement |
| 62 | /// for-statement |
| 63 | /// |
| 64 | /// expression-statement: |
| 65 | /// expression[opt] ';' |
| 66 | /// |
| 67 | /// jump-statement: |
| 68 | /// 'goto' identifier ';' |
| 69 | /// 'continue' ';' |
| 70 | /// 'break' ';' |
| 71 | /// 'return' expression[opt] ';' |
| 72 | /// [GNU] 'goto' '*' expression ';' |
| 73 | /// |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 74 | /// [OBC] objc-throw-statement: |
| 75 | /// [OBC] '@' 'throw' expression ';' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | /// [OBC] '@' 'throw' ';' |
| 77 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 78 | StmtResult |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 79 | Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement, |
| 80 | SourceLocation *TrailingElseLoc) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 81 | |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 82 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 83 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 84 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 85 | MaybeParseCXX0XAttributes(Attrs, 0, /*MightBeObjCMessageSend*/ true); |
| 86 | |
| 87 | StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts, |
| 88 | OnlyStatement, TrailingElseLoc, Attrs); |
| 89 | |
| 90 | assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) && |
| 91 | "attributes on empty statement"); |
| 92 | |
| 93 | if (Attrs.empty() || Res.isInvalid()) |
| 94 | return Res; |
| 95 | |
| 96 | return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range); |
| 97 | } |
| 98 | |
| 99 | StmtResult |
| 100 | Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts, |
| 101 | bool OnlyStatement, SourceLocation *TrailingElseLoc, |
| 102 | ParsedAttributesWithRange &Attrs) { |
| 103 | const char *SemiError = 0; |
| 104 | StmtResult Res; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 105 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | // Cases in this switch statement should fall through if the parser expects |
| 107 | // the token to end in a semicolon (in which case SemiError should be set), |
| 108 | // or they directly 'return;' if not. |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 109 | Retry: |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 110 | tok::TokenKind Kind = Tok.getKind(); |
| 111 | SourceLocation AtLoc; |
| 112 | switch (Kind) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 113 | case tok::at: // May be a @try or @throw statement |
| 114 | { |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 115 | ProhibitAttributes(Attrs); // TODO: is it correct? |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 116 | AtLoc = ConsumeToken(); // consume @ |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 117 | return ParseObjCAtStatement(AtLoc); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 118 | } |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 120 | case tok::code_completion: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 121 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 122 | cutOffParsing(); |
| 123 | return StmtError(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 125 | case tok::identifier: { |
| 126 | Token Next = NextToken(); |
| 127 | if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement |
Argyrios Kyrtzidis | b9f930d | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 128 | // identifier ':' statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 129 | return ParseLabeledStatement(Attrs); |
Argyrios Kyrtzidis | b9f930d | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 130 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 132 | if (Next.isNot(tok::coloncolon)) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 133 | CXXScopeSpec SS; |
| 134 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
| 135 | SourceLocation NameLoc = Tok.getLocation(); |
Richard Trieu | 950be71 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 136 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 137 | if (getLangOpts().CPlusPlus) |
Richard Trieu | 950be71 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 138 | CheckForTemplateAndDigraph(Next, ParsedType(), |
| 139 | /*EnteringContext=*/false, *Name, SS); |
| 140 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 141 | Sema::NameClassification Classification |
| 142 | = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next); |
| 143 | switch (Classification.getKind()) { |
| 144 | case Sema::NC_Keyword: |
| 145 | // The identifier was corrected to a keyword. Update the token |
| 146 | // to this keyword, and try again. |
| 147 | if (Name->getTokenID() != tok::identifier) { |
| 148 | Tok.setIdentifierInfo(Name); |
| 149 | Tok.setKind(Name->getTokenID()); |
| 150 | goto Retry; |
| 151 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 153 | // Fall through via the normal error path. |
| 154 | // FIXME: This seems like it could only happen for context-sensitive |
| 155 | // keywords. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 156 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 157 | case Sema::NC_Error: |
| 158 | // Handle errors here by skipping up to the next semicolon or '}', and |
| 159 | // eat the semicolon if that's what stopped us. |
| 160 | SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 161 | if (Tok.is(tok::semi)) |
| 162 | ConsumeToken(); |
| 163 | return StmtError(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 165 | case Sema::NC_Unknown: |
| 166 | // Either we don't know anything about this identifier, or we know that |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 167 | // we're in a syntactic context we haven't handled yet. |
| 168 | break; |
| 169 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 170 | case Sema::NC_Type: |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 171 | Tok.setKind(tok::annot_typename); |
| 172 | setTypeAnnotation(Tok, Classification.getType()); |
| 173 | Tok.setAnnotationEndLoc(NameLoc); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 174 | PP.AnnotateCachedTokens(Tok); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 175 | break; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 177 | case Sema::NC_Expression: |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 178 | Tok.setKind(tok::annot_primary_expr); |
| 179 | setExprAnnotation(Tok, Classification.getExpression()); |
| 180 | Tok.setAnnotationEndLoc(NameLoc); |
| 181 | PP.AnnotateCachedTokens(Tok); |
| 182 | break; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 184 | case Sema::NC_TypeTemplate: |
| 185 | case Sema::NC_FunctionTemplate: { |
| 186 | ConsumeToken(); // the identifier |
| 187 | UnqualifiedId Id; |
| 188 | Id.setIdentifier(Name, NameLoc); |
| 189 | if (AnnotateTemplateIdToken( |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 190 | TemplateTy::make(Classification.getTemplateName()), |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 191 | Classification.getTemplateNameKind(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 192 | SS, SourceLocation(), Id, |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 193 | /*AllowTypeAnnotation=*/false)) { |
| 194 | // Handle errors here by skipping up to the next semicolon or '}', and |
| 195 | // eat the semicolon if that's what stopped us. |
| 196 | SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 197 | if (Tok.is(tok::semi)) |
| 198 | ConsumeToken(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 199 | return StmtError(); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 200 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 201 | |
| 202 | // If the next token is '::', jump right into parsing a |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 203 | // nested-name-specifier. We don't want to leave the template-id |
| 204 | // hanging. |
| 205 | if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){ |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 206 | // Handle errors here by skipping up to the next semicolon or '}', and |
| 207 | // eat the semicolon if that's what stopped us. |
| 208 | SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 209 | if (Tok.is(tok::semi)) |
| 210 | ConsumeToken(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 211 | return StmtError(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 212 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 214 | // We've annotated a template-id, so try again now. |
| 215 | goto Retry; |
| 216 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 218 | case Sema::NC_NestedNameSpecifier: |
| 219 | // FIXME: Implement this! |
| 220 | break; |
| 221 | } |
| 222 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 224 | // Fall through |
| 225 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 226 | |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 227 | default: { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 228 | if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) { |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 229 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 230 | DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext, |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 231 | DeclEnd, Attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 232 | return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd); |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | Diag(Tok, diag::err_expected_statement); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 237 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 240 | return ParseExprStatement(); |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 241 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 242 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | case tok::kw_case: // C99 6.8.1: labeled-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 244 | return ParseCaseStatement(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | case tok::kw_default: // C99 6.8.1: labeled-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 246 | return ParseDefaultStatement(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 247 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | case tok::l_brace: // C99 6.8.2: compound-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 249 | return ParseCompoundStatement(); |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 250 | case tok::semi: { // C99 6.8.3p3: expression[opt] ';' |
Argyrios Kyrtzidis | e2ca828 | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 251 | bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro(); |
| 252 | return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro); |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 253 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | case tok::kw_if: // C99 6.8.4.1: if-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 256 | return ParseIfStatement(TrailingElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 258 | return ParseSwitchStatement(TrailingElseLoc); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | case tok::kw_while: // C99 6.8.5.1: while-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 261 | return ParseWhileStatement(TrailingElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | case tok::kw_do: // C99 6.8.5.2: do-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 263 | Res = ParseDoStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 264 | SemiError = "do/while"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | break; |
| 266 | case tok::kw_for: // C99 6.8.5.3: for-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 267 | return ParseForStatement(TrailingElseLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | |
| 269 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 270 | Res = ParseGotoStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 271 | SemiError = "goto"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | break; |
| 273 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 274 | Res = ParseContinueStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 275 | SemiError = "continue"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | break; |
| 277 | case tok::kw_break: // C99 6.8.6.3: break-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 278 | Res = ParseBreakStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 279 | SemiError = "break"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | break; |
| 281 | case tok::kw_return: // C99 6.8.6.4: return-statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 282 | Res = ParseReturnStatement(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 283 | SemiError = "return"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | break; |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 285 | |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 286 | case tok::kw_asm: { |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 287 | ProhibitAttributes(Attrs); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 288 | bool msAsm = false; |
| 289 | Res = ParseAsmStatement(msAsm); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 290 | Res = Actions.ActOnFinishFullStmt(Res.get()); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 291 | if (msAsm) return move(Res); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 292 | SemiError = "asm"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | break; |
| 294 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 295 | |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 296 | case tok::kw_try: // C++ 15: try-block |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 297 | return ParseCXXTryBlock(); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 298 | |
| 299 | case tok::kw___try: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 300 | ProhibitAttributes(Attrs); // TODO: is it correct? |
| 301 | return ParseSEHTryBlock(); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 302 | |
| 303 | case tok::annot_pragma_vis: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 304 | ProhibitAttributes(Attrs); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 305 | HandlePragmaVisibility(); |
| 306 | return StmtEmpty(); |
| 307 | |
| 308 | case tok::annot_pragma_pack: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 309 | ProhibitAttributes(Attrs); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 310 | HandlePragmaPack(); |
| 311 | return StmtEmpty(); |
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 | |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 326 | return move(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(); |
| 343 | return StmtError(); |
| 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); |
| 359 | return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get())); |
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 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 381 | StmtResult TryBlock(ParseCompoundStatement()); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 382 | if(TryBlock.isInvalid()) |
| 383 | return move(TryBlock); |
| 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()) |
| 398 | return move(Handler); |
| 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()) |
| 443 | return move(Block); |
| 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()) |
| 460 | return move(Block); |
| 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()) |
| 605 | TopLevelCase = move(Case); |
| 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. |
Chris Lattner | 26140c6 | 2009-03-04 18:24:58 +0000 | [diff] [blame] | 638 | return move(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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 730 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 731 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 732 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 733 | /// stack. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 734 | StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 736 | Tok.getLocation(), |
| 737 | "in compound statement ('{}')"); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 738 | InMessageExpressionRAIIObject InMessage(*this, false); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 739 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 740 | if (T.consumeOpen()) |
| 741 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 742 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 743 | Sema::CompoundScopeRAII CompoundScope(Actions); |
| 744 | |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 745 | StmtVector Stmts(Actions); |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 747 | // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are |
| 748 | // only allowed at the start of a compound stmt regardless of the language. |
| 749 | while (Tok.is(tok::kw___label__)) { |
| 750 | SourceLocation LabelLoc = ConsumeToken(); |
| 751 | Diag(LabelLoc, diag::ext_gnu_local_label); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 753 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 754 | while (1) { |
| 755 | if (Tok.isNot(tok::identifier)) { |
| 756 | Diag(Tok, diag::err_expected_ident); |
| 757 | break; |
| 758 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 759 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 760 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 761 | SourceLocation IdLoc = ConsumeToken(); |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 762 | DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc)); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 764 | if (!Tok.is(tok::comma)) |
| 765 | break; |
| 766 | ConsumeToken(); |
| 767 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 768 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 769 | DeclSpec DS(AttrFactory); |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 770 | DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
| 771 | DeclsInGroup.data(), DeclsInGroup.size()); |
| 772 | StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 773 | |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame^] | 774 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 775 | if (R.isUsable()) |
| 776 | Stmts.push_back(R.release()); |
| 777 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 779 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 780 | if (Tok.is(tok::annot_pragma_unused)) { |
| 781 | HandlePragmaUnused(); |
| 782 | continue; |
| 783 | } |
| 784 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 785 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 786 | Tok.is(tok::kw___if_not_exists))) { |
| 787 | ParseMicrosoftIfExistsStatement(Stmts); |
| 788 | continue; |
| 789 | } |
| 790 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 791 | StmtResult R; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 792 | if (Tok.isNot(tok::kw___extension__)) { |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 793 | R = ParseStatementOrDeclaration(Stmts, false); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 794 | } else { |
| 795 | // __extension__ can start declarations and it can also be a unary |
| 796 | // operator for expressions. Consume multiple __extension__ markers here |
| 797 | // until we can determine which is which. |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 798 | // FIXME: This loses extension expressions in the AST! |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 799 | SourceLocation ExtLoc = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 800 | while (Tok.is(tok::kw___extension__)) |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 801 | ConsumeToken(); |
Chris Lattner | 39146d6 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 802 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 803 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 804 | MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 806 | // If this is the start of a declaration, parse it as such. |
Argyrios Kyrtzidis | 5404a15 | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 807 | if (isDeclarationStatement()) { |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 808 | // __extension__ silences extension warnings in the subdeclaration. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 809 | // FIXME: Save the __extension__ on the decl as a node somehow? |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 810 | ExtensionRAIIObject O(Diags); |
| 811 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 812 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 813 | DeclGroupPtrTy Res = ParseDeclaration(Stmts, |
| 814 | Declarator::BlockContext, DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 815 | attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 816 | R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 817 | } else { |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 818 | // Otherwise this was a unary __extension__ marker. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 819 | ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); |
Chris Lattner | 043a0b5 | 2008-03-13 06:32:11 +0000 | [diff] [blame] | 820 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 821 | if (Res.isInvalid()) { |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 822 | SkipUntil(tok::semi); |
| 823 | continue; |
| 824 | } |
Sebastian Redl | f512e82 | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 825 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 826 | // FIXME: Use attributes? |
Chris Lattner | 39146d6 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 827 | // Eat the semicolon at the end of stmt and convert the expr into a |
| 828 | // statement. |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 829 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 830 | R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get())); |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 834 | if (R.isUsable()) |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 835 | Stmts.push_back(R.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 837 | |
Argyrios Kyrtzidis | 5d5ed59 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 838 | SourceLocation CloseLoc = Tok.getLocation(); |
| 839 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 840 | // We broke out of the while loop because we found a '}' or EOF. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 841 | if (Tok.isNot(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 842 | Diag(Tok, diag::err_expected_rbrace); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 843 | Diag(T.getOpenLocation(), diag::note_matching) << "{"; |
Argyrios Kyrtzidis | 5d5ed59 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 844 | // Recover by creating a compound statement with what we parsed so far, |
| 845 | // instead of dropping everything and returning StmtError(); |
| 846 | } else { |
| 847 | if (!T.consumeClose()) |
| 848 | CloseLoc = T.getCloseLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 849 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 850 | |
Argyrios Kyrtzidis | 5d5ed59 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 851 | return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 852 | move_arg(Stmts), isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 855 | /// ParseParenExprOrCondition: |
| 856 | /// [C ] '(' expression ')' |
Chris Lattner | ff871fb | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 857 | /// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true] |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 858 | /// |
| 859 | /// This function parses and performs error recovery on the specified condition |
| 860 | /// or expression (depending on whether we're in C++ or C mode). This function |
| 861 | /// goes out of its way to recover well. It returns true if there was a parser |
| 862 | /// error (the right paren couldn't be found), which indicates that the caller |
| 863 | /// should try to recover harder. It returns false if the condition is |
| 864 | /// successfully parsed. Note that a successful parse can still have semantic |
| 865 | /// errors in the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 866 | bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 867 | Decl *&DeclResult, |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 868 | SourceLocation Loc, |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 869 | bool ConvertToBoolean) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 870 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 871 | T.consumeOpen(); |
| 872 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 873 | if (getLangOpts().CPlusPlus) |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 874 | ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 875 | else { |
| 876 | ExprResult = ParseExpression(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 877 | DeclResult = 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 879 | // If required, convert to a boolean value. |
| 880 | if (!ExprResult.isInvalid() && ConvertToBoolean) |
| 881 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 882 | = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get()); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 883 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 885 | // If the parser was confused by the condition and we don't have a ')', try to |
| 886 | // recover by skipping ahead to a semi and bailing out. If condexp is |
| 887 | // semantically invalid but we have well formed code, keep going. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 888 | if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) { |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 889 | SkipUntil(tok::semi); |
| 890 | // Skipping may have stopped if it found the containing ')'. If so, we can |
| 891 | // continue parsing the if statement. |
| 892 | if (Tok.isNot(tok::r_paren)) |
| 893 | return true; |
| 894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 896 | // Otherwise the condition is valid or the rparen is present. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 897 | T.consumeClose(); |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
| 901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 902 | /// ParseIfStatement |
| 903 | /// if-statement: [C99 6.8.4.1] |
| 904 | /// 'if' '(' expression ')' statement |
| 905 | /// 'if' '(' expression ')' statement 'else' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 906 | /// [C++] 'if' '(' condition ')' statement |
| 907 | /// [C++] 'if' '(' condition ')' statement 'else' statement |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 908 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 909 | StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 910 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 911 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
| 912 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 913 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 914 | Diag(Tok, diag::err_expected_lparen_after) << "if"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 915 | SkipUntil(tok::semi); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 916 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | } |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 918 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 919 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 921 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 922 | // the case for C90. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 923 | // |
| 924 | // C++ 6.4p3: |
| 925 | // A name introduced by a declaration in a condition is in scope from its |
| 926 | // point of declaration until the end of the substatements controlled by the |
| 927 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 928 | // C++ 3.3.2p4: |
| 929 | // Names declared in the for-init-statement, and in the condition of if, |
| 930 | // while, for, and switch statements are local to the if, while, for, or |
| 931 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 932 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 933 | ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 934 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 935 | // Parse the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 936 | ExprResult CondExp; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 937 | Decl *CondVar = 0; |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 938 | if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true)) |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 939 | return StmtError(); |
Chris Lattner | 18914bc | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 940 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 941 | FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 943 | // 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] | 944 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 945 | // 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] | 946 | // |
| 947 | // C++ 6.4p1: |
| 948 | // The substatement in a selection-statement (each substatement, in the else |
| 949 | // form of the if statement) implicitly defines a local scope. |
| 950 | // |
| 951 | // For C++ we create a scope for the condition and a new scope for |
| 952 | // substatements because: |
| 953 | // -When the 'then' scope exits, we want the condition declaration to still be |
| 954 | // active for the 'else' scope too. |
| 955 | // -Sema will detect name clashes by considering declarations of a |
| 956 | // 'ControlScope' as part of its direct subscope. |
| 957 | // -If we wanted the condition and substatement to be in the same scope, we |
| 958 | // would have to notify ParseStatement not to create a new scope. It's |
| 959 | // simpler to let it create a new scope. |
| 960 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 962 | C99orCXX && Tok.isNot(tok::l_brace)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 963 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 964 | // Read the 'then' stmt. |
| 965 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 966 | |
| 967 | SourceLocation InnerStatementTrailingElseLoc; |
| 968 | StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 970 | // Pop the 'if' scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 971 | InnerScope.Exit(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 972 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 973 | // If it has an else, parse it. |
| 974 | SourceLocation ElseLoc; |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 975 | SourceLocation ElseStmtLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 976 | StmtResult ElseStmt; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 978 | if (Tok.is(tok::kw_else)) { |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 979 | if (TrailingElseLoc) |
| 980 | *TrailingElseLoc = Tok.getLocation(); |
| 981 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | ElseLoc = ConsumeToken(); |
Chris Lattner | 966c78b | 2010-04-12 06:12:50 +0000 | [diff] [blame] | 983 | ElseStmtLoc = Tok.getLocation(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 985 | // 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] | 986 | // there is no compound stmt. C90 does not have this clause. We only do |
| 987 | // this if the body isn't a compound statement to avoid push/pop in common |
| 988 | // cases. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 989 | // |
| 990 | // C++ 6.4p1: |
| 991 | // The substatement in a selection-statement (each substatement, in the else |
| 992 | // form of the if statement) implicitly defines a local scope. |
| 993 | // |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 994 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 995 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 996 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | ElseStmt = ParseStatement(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 998 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 999 | // Pop the 'else' scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1000 | InnerScope.Exit(); |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 1001 | } else if (Tok.is(tok::code_completion)) { |
| 1002 | Actions.CodeCompleteAfterIf(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1003 | cutOffParsing(); |
| 1004 | return StmtError(); |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1005 | } else if (InnerStatementTrailingElseLoc.isValid()) { |
| 1006 | Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1009 | IfScope.Exit(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 18914bc | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 1011 | // If the condition was invalid, discard the if statement. We could recover |
| 1012 | // better by replacing it with a valid expr, but don't do that yet. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1013 | if (CondExp.isInvalid() && !CondVar) |
Chris Lattner | 18914bc | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 1014 | return StmtError(); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1016 | // 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] | 1017 | // 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] | 1018 | // part. If both are invalid, return error. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1019 | if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || |
| 1020 | (ThenStmt.isInvalid() && ElseStmt.get() == 0) || |
| 1021 | (ThenStmt.get() == 0 && ElseStmt.isInvalid())) { |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1022 | // 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] | 1023 | return StmtError(); |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1024 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1026 | // Now if either are invalid, replace with a ';'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1027 | if (ThenStmt.isInvalid()) |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1028 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1029 | if (ElseStmt.isInvalid()) |
Chris Lattner | b96728d | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1030 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1031 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1032 | return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(), |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 1033 | ElseLoc, ElseStmt.get()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /// ParseSwitchStatement |
| 1037 | /// switch-statement: |
| 1038 | /// 'switch' '(' expression ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1039 | /// [C++] 'switch' '(' condition ')' statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1040 | StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1041 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
| 1043 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1044 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1045 | Diag(Tok, diag::err_expected_lparen_after) << "switch"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1046 | SkipUntil(tok::semi); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1047 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | } |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1049 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1050 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1052 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 1053 | // not the case for C90. Start the switch scope. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1054 | // |
| 1055 | // C++ 6.4p3: |
| 1056 | // A name introduced by a declaration in a condition is in scope from its |
| 1057 | // point of declaration until the end of the substatements controlled by the |
| 1058 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1059 | // C++ 3.3.2p4: |
| 1060 | // Names declared in the for-init-statement, and in the condition of if, |
| 1061 | // while, for, and switch statements are local to the if, while, for, or |
| 1062 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1063 | // |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 1064 | unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope; |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1065 | if (C99orCXX) |
| 1066 | ScopeFlags |= Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1067 | ParseScope SwitchScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Parse the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1070 | ExprResult Cond; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1071 | Decl *CondVar = 0; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1072 | if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false)) |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1073 | return StmtError(); |
Eli Friedman | 2342ef7 | 2008-12-17 22:19:57 +0000 | [diff] [blame] | 1074 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1075 | StmtResult Switch |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1076 | = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1078 | if (Switch.isInvalid()) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1079 | // Skip the switch body. |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1080 | // FIXME: This is not optimal recovery, but parsing the body is more |
| 1081 | // dangerous due to the presence of case and default statements, which |
| 1082 | // will have no place to connect back with the switch. |
Douglas Gregor | 4186ff4 | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 1083 | if (Tok.is(tok::l_brace)) { |
| 1084 | ConsumeBrace(); |
| 1085 | SkipUntil(tok::r_brace, false, false); |
| 1086 | } else |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1087 | SkipUntil(tok::semi); |
| 1088 | return move(Switch); |
| 1089 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1091 | // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1092 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1093 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1094 | // |
| 1095 | // C++ 6.4p1: |
| 1096 | // The substatement in a selection-statement (each substatement, in the else |
| 1097 | // form of the if statement) implicitly defines a local scope. |
| 1098 | // |
| 1099 | // See comments in ParseIfStatement for why we create a scope for the |
| 1100 | // condition and a new scope for substatement in C++. |
| 1101 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1103 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1104 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1105 | // Read the body statement. |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1106 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 7e52de4 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1108 | // Pop the scopes. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1109 | InnerScope.Exit(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1110 | SwitchScope.Exit(); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1111 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1112 | if (Body.isInvalid()) { |
Chris Lattner | 7e52de4 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1113 | // FIXME: Remove the case statement list from the Switch statement. |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1114 | |
| 1115 | // Put the synthesized null statement on the same line as the end of switch |
| 1116 | // condition. |
| 1117 | SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd(); |
| 1118 | Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation); |
| 1119 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1120 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1121 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /// ParseWhileStatement |
| 1125 | /// while-statement: [C99 6.8.5.1] |
| 1126 | /// 'while' '(' expression ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1127 | /// [C++] 'while' '(' condition ')' statement |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1128 | StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1129 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1130 | SourceLocation WhileLoc = Tok.getLocation(); |
| 1131 | ConsumeToken(); // eat the 'while'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1133 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1134 | Diag(Tok, diag::err_expected_lparen_after) << "while"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | SkipUntil(tok::semi); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1136 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1137 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1138 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1139 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1141 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 1142 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1143 | // |
| 1144 | // C++ 6.4p3: |
| 1145 | // A name introduced by a declaration in a condition is in scope from its |
| 1146 | // point of declaration until the end of the substatements controlled by the |
| 1147 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1148 | // C++ 3.3.2p4: |
| 1149 | // Names declared in the for-init-statement, and in the condition of if, |
| 1150 | // while, for, and switch statements are local to the if, while, for, or |
| 1151 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1152 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1153 | unsigned ScopeFlags; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1154 | if (C99orCXX) |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1155 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 1156 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1157 | else |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1158 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 1159 | ParseScope WhileScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Parse the condition. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1162 | ExprResult Cond; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1163 | Decl *CondVar = 0; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1164 | if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true)) |
Chris Lattner | 15ff111 | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1165 | return StmtError(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1166 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1167 | FullExprArg FullCond(Actions.MakeFullExpr(Cond.get())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1169 | // 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] | 1170 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1171 | // 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] | 1172 | // |
| 1173 | // C++ 6.5p2: |
| 1174 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1175 | // which is entered and exited each time through the loop. |
| 1176 | // |
| 1177 | // See comments in ParseIfStatement for why we create a scope for the |
| 1178 | // condition and a new scope for substatement in C++. |
| 1179 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1181 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | // Read the body statement. |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1184 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1186 | // Pop the body scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1187 | InnerScope.Exit(); |
| 1188 | WhileScope.Exit(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1189 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1190 | if ((Cond.isInvalid() && !CondVar) || Body.isInvalid()) |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1191 | return StmtError(); |
| 1192 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1193 | return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /// ParseDoStatement |
| 1197 | /// do-statement: [C99 6.8.5.2] |
| 1198 | /// 'do' statement 'while' '(' expression ')' ';' |
| 1199 | /// Note: this lets the caller parse the end ';'. |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1200 | StmtResult Parser::ParseDoStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1201 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1204 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 1205 | // the case for C90. Start the loop scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1206 | unsigned ScopeFlags; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1207 | if (getLangOpts().C99) |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1208 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1209 | else |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1210 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1211 | |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1212 | ParseScope DoScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1214 | // 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] | 1215 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1216 | // 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] | 1217 | // |
| 1218 | // C++ 6.5p2: |
| 1219 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1220 | // which is entered and exited each time through the loop. |
| 1221 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1222 | ParseScope InnerScope(this, Scope::DeclScope, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1223 | (getLangOpts().C99 || getLangOpts().CPlusPlus) && |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1224 | Tok.isNot(tok::l_brace)); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1225 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | // Read the body statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1227 | StmtResult Body(ParseStatement()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1229 | // Pop the body scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1230 | InnerScope.Exit(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1232 | if (Tok.isNot(tok::kw_while)) { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1233 | if (!Body.isInvalid()) { |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1234 | Diag(Tok, diag::err_expected_while); |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 1235 | Diag(DoLoc, diag::note_matching) << "do"; |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1236 | SkipUntil(tok::semi, false, true); |
| 1237 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1238 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1239 | } |
| 1240 | SourceLocation WhileLoc = ConsumeToken(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1242 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1243 | Diag(Tok, diag::err_expected_lparen_after) << "do/while"; |
Chris Lattner | 1950440 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1244 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1245 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | ff871fb | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 1248 | // Parse the parenthesized condition. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1249 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1250 | T.consumeOpen(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1251 | ExprResult Cond = ParseExpression(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1252 | T.consumeClose(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1253 | DoScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1254 | |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1255 | if (Cond.isInvalid() || Body.isInvalid()) |
| 1256 | return StmtError(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1258 | return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(), |
| 1259 | Cond.get(), T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /// ParseForStatement |
| 1263 | /// for-statement: [C99 6.8.5.3] |
| 1264 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 1265 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1266 | /// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')' |
| 1267 | /// [C++] statement |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1268 | /// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1269 | /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement |
| 1270 | /// [OBJC2] 'for' '(' expr 'in' expr ')' statement |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1271 | /// |
| 1272 | /// [C++] for-init-statement: |
| 1273 | /// [C++] expression-statement |
| 1274 | /// [C++] simple-declaration |
| 1275 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1276 | /// [C++0x] for-range-declaration: |
| 1277 | /// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1278 | /// [C++0x] for-range-initializer: |
| 1279 | /// [C++0x] expression |
| 1280 | /// [C++0x] braced-init-list [TODO] |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1281 | StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1282 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1283 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1285 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1286 | Diag(Tok, diag::err_expected_lparen_after) << "for"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1287 | SkipUntil(tok::semi); |
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 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1290 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1291 | bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || getLangOpts().ObjC1; |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1293 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 1294 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1295 | // |
| 1296 | // C++ 6.4p3: |
| 1297 | // A name introduced by a declaration in a condition is in scope from its |
| 1298 | // point of declaration until the end of the substatements controlled by the |
| 1299 | // condition. |
Argyrios Kyrtzidis | 14d08c0 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1300 | // C++ 3.3.2p4: |
| 1301 | // Names declared in the for-init-statement, and in the condition of if, |
| 1302 | // while, for, and switch statements are local to the if, while, for, or |
| 1303 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 488d37e | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1304 | // C++ 6.5.3p1: |
| 1305 | // Names declared in the for-init-statement are in the same declarative-region |
| 1306 | // as those declared in the condition. |
| 1307 | // |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1308 | unsigned ScopeFlags; |
Chris Lattner | 4d00f2a | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1309 | if (C99orCXXorObjC) |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1310 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 1311 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1312 | else |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1313 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 1314 | |
| 1315 | ParseScope ForScope(this, ScopeFlags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1316 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1317 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1318 | T.consumeOpen(); |
| 1319 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1320 | ExprResult Value; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1321 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1322 | bool ForEach = false, ForRange = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1323 | StmtResult FirstPart; |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1324 | bool SecondPartIsInvalid = false; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1325 | FullExprArg SecondPart(Actions); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1326 | ExprResult Collection; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1327 | ForRangeInit ForRangeInit; |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1328 | FullExprArg ThirdPart(Actions); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1329 | Decl *SecondVar = 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1331 | if (Tok.is(tok::code_completion)) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1332 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1333 | C99orCXXorObjC? Sema::PCC_ForInit |
| 1334 | : Sema::PCC_Expression); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1335 | cutOffParsing(); |
| 1336 | return StmtError(); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1337 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1339 | // Parse the first part of the for specifier. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1340 | if (Tok.is(tok::semi)) { // for (; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | // no first part, eat the ';'. |
| 1342 | ConsumeToken(); |
Eli Friedman | 9490ab4 | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 1343 | } else if (isForInitDeclaration()) { // for (int X = 4; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | // Parse declaration, which eats the ';'. |
Chris Lattner | 4d00f2a | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1345 | if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1347 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1348 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1349 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1350 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1351 | // 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] | 1352 | bool MightBeForRangeStmt = getLangOpts().CPlusPlus; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1353 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
| 1354 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1355 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1356 | StmtVector Stmts(Actions); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1357 | DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1358 | DeclEnd, attrs, false, |
| 1359 | MightBeForRangeStmt ? |
| 1360 | &ForRangeInit : 0); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1361 | FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1363 | if (ForRangeInit.ParsedForRangeDecl()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1364 | Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1365 | diag::warn_cxx98_compat_for_range : diag::ext_for_range); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 1366 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1367 | ForRange = true; |
| 1368 | } else if (Tok.is(tok::semi)) { // for (int x = 4; |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1369 | ConsumeToken(); |
| 1370 | } else if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 1371 | Actions.ActOnForEachDeclStmt(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | // ObjC: for (id x in expr) |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1373 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1374 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1375 | if (Tok.is(tok::code_completion)) { |
| 1376 | Actions.CodeCompleteObjCForCollection(getCurScope(), DG); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1377 | cutOffParsing(); |
| 1378 | return StmtError(); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1379 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1380 | Collection = ParseExpression(); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1381 | } else { |
| 1382 | Diag(Tok, diag::err_expected_semi_for); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1383 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1384 | } else { |
| 1385 | Value = ParseExpression(); |
| 1386 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1387 | ForEach = isTokIdentifier_in(); |
| 1388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | // Turn the expression into a stmt. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1390 | if (!Value.isInvalid()) { |
| 1391 | if (ForEach) |
| 1392 | FirstPart = Actions.ActOnForEachLValueExpr(Value.get()); |
| 1393 | else |
| 1394 | FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get())); |
| 1395 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1397 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1398 | ConsumeToken(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1399 | } else if (ForEach) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1400 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1401 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1402 | if (Tok.is(tok::code_completion)) { |
| 1403 | Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1404 | cutOffParsing(); |
| 1405 | return StmtError(); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1406 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1407 | Collection = ParseExpression(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1408 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) { |
Richard Smith | a44854a | 2011-12-20 22:56:20 +0000 | [diff] [blame] | 1409 | // User tried to write the reasonable, but ill-formed, for-range-statement |
| 1410 | // for (expr : expr) { ... } |
| 1411 | Diag(Tok, diag::err_for_range_expected_decl) |
| 1412 | << FirstPart.get()->getSourceRange(); |
| 1413 | SkipUntil(tok::r_paren, false, true); |
| 1414 | SecondPartIsInvalid = true; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1415 | } else { |
Douglas Gregor | b72c778 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1416 | if (!Value.isInvalid()) { |
| 1417 | Diag(Tok, diag::err_expected_semi_for); |
| 1418 | } else { |
| 1419 | // Skip until semicolon or rparen, don't consume it. |
| 1420 | SkipUntil(tok::r_paren, true, true); |
| 1421 | if (Tok.is(tok::semi)) |
| 1422 | ConsumeToken(); |
| 1423 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | } |
| 1425 | } |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1426 | if (!ForEach && !ForRange) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1427 | assert(!SecondPart.get() && "Shouldn't have a second expression yet."); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1428 | // Parse the second part of the for specifier. |
| 1429 | if (Tok.is(tok::semi)) { // for (...;; |
| 1430 | // no second part. |
Douglas Gregor | b72c778 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1431 | } else if (Tok.is(tok::r_paren)) { |
| 1432 | // missing both semicolons. |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1433 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1434 | ExprResult Second; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1435 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1436 | ParseCXXCondition(Second, SecondVar, ForLoc, true); |
| 1437 | else { |
| 1438 | Second = ParseExpression(); |
| 1439 | if (!Second.isInvalid()) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1440 | Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1441 | Second.get()); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1442 | } |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1443 | SecondPartIsInvalid = Second.isInvalid(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1444 | SecondPart = Actions.MakeFullExpr(Second.get()); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1445 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1446 | |
Douglas Gregor | b72c778 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1447 | if (Tok.isNot(tok::semi)) { |
| 1448 | if (!SecondPartIsInvalid || SecondVar) |
| 1449 | Diag(Tok, diag::err_expected_semi_for); |
| 1450 | else |
| 1451 | // Skip until semicolon or rparen, don't consume it. |
| 1452 | SkipUntil(tok::r_paren, true, true); |
| 1453 | } |
| 1454 | |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1455 | if (Tok.is(tok::semi)) { |
| 1456 | ConsumeToken(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1457 | } |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1458 | |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1459 | // Parse the third part of the for specifier. |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1460 | if (Tok.isNot(tok::r_paren)) { // for (...;...;) |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1461 | ExprResult Third = ParseExpression(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1462 | ThirdPart = Actions.MakeFullExpr(Third.take()); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1463 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1464 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1465 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1466 | T.consumeClose(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1467 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1468 | // We need to perform most of the semantic analysis for a C++0x for-range |
| 1469 | // statememt before parsing the body, in order to be able to deduce the type |
| 1470 | // of an auto-typed loop variable. |
| 1471 | StmtResult ForRangeStmt; |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1472 | if (ForRange) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1473 | ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(), |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1474 | FirstPart.take(), |
| 1475 | ForRangeInit.ColonLoc, |
| 1476 | ForRangeInit.RangeExpr.get(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1477 | T.getCloseLocation()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1478 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Similarly, we need to do the semantic analysis for a for-range |
| 1481 | // statement immediately in order to close over temporaries correctly. |
| 1482 | } else if (ForEach) { |
| 1483 | if (!Collection.isInvalid()) |
| 1484 | Collection = |
| 1485 | Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take()); |
| 1486 | } |
| 1487 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1488 | // 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] | 1489 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1490 | // 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] | 1491 | // |
| 1492 | // C++ 6.5p2: |
| 1493 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1494 | // which is entered and exited each time through the loop. |
| 1495 | // |
| 1496 | // See comments in ParseIfStatement for why we create a scope for |
| 1497 | // for-init-statement/condition and a new scope for substatement in C++. |
| 1498 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | ParseScope InnerScope(this, Scope::DeclScope, |
Chris Lattner | 4d00f2a | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1500 | C99orCXXorObjC && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1501 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | // Read the body statement. |
Nico Weber | 5cb94a7 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1503 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1504 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1505 | // Pop the body scope if needed. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1506 | InnerScope.Exit(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1507 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | // Leave the for-scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1509 | ForScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1510 | |
| 1511 | if (Body.isInvalid()) |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1512 | return StmtError(); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1513 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1514 | if (ForEach) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1515 | return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(), |
| 1516 | FirstPart.take(), |
| 1517 | Collection.take(), |
| 1518 | T.getCloseLocation(), |
| 1519 | Body.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1521 | if (ForRange) |
| 1522 | return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take()); |
| 1523 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1524 | return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(), |
| 1525 | SecondPart, SecondVar, ThirdPart, |
| 1526 | T.getCloseLocation(), Body.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /// ParseGotoStatement |
| 1530 | /// jump-statement: |
| 1531 | /// 'goto' identifier ';' |
| 1532 | /// [GNU] 'goto' '*' expression ';' |
| 1533 | /// |
| 1534 | /// Note: this lets the caller parse the end ';'. |
| 1535 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1536 | StmtResult Parser::ParseGotoStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1537 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1538 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1539 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1540 | StmtResult Res; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1541 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1542 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
| 1543 | Tok.getLocation()); |
| 1544 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1545 | ConsumeToken(); |
Eli Friedman | f01fdff | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 1546 | } else if (Tok.is(tok::star)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1547 | // GNU indirect goto extension. |
| 1548 | Diag(Tok, diag::ext_gnu_indirect_goto); |
| 1549 | SourceLocation StarLoc = ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1550 | ExprResult R(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1551 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1552 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1553 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1555 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take()); |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 1556 | } else { |
| 1557 | Diag(Tok, diag::err_expected_ident); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1558 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1560 | |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1561 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /// ParseContinueStatement |
| 1565 | /// jump-statement: |
| 1566 | /// 'continue' ';' |
| 1567 | /// |
| 1568 | /// Note: this lets the caller parse the end ';'. |
| 1569 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1570 | StmtResult Parser::ParseContinueStatement() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1572 | return Actions.ActOnContinueStmt(ContinueLoc, getCurScope()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /// ParseBreakStatement |
| 1576 | /// jump-statement: |
| 1577 | /// 'break' ';' |
| 1578 | /// |
| 1579 | /// Note: this lets the caller parse the end ';'. |
| 1580 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1581 | StmtResult Parser::ParseBreakStatement() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1583 | return Actions.ActOnBreakStmt(BreakLoc, getCurScope()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /// ParseReturnStatement |
| 1587 | /// jump-statement: |
| 1588 | /// 'return' expression[opt] ';' |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1589 | StmtResult Parser::ParseReturnStatement() { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1590 | assert(Tok.is(tok::kw_return) && "Not a return stmt!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1592 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1593 | ExprResult R; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1594 | if (Tok.isNot(tok::semi)) { |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1595 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1596 | Actions.CodeCompleteReturn(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1597 | cutOffParsing(); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1598 | return StmtError(); |
| 1599 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1600 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1601 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) { |
Douglas Gregor | 6f4596c | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1602 | R = ParseInitializer(); |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1603 | if (R.isUsable()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1604 | Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1605 | diag::warn_cxx98_compat_generalized_initializer_lists : |
| 1606 | diag::ext_generalized_initializer_lists) |
Douglas Gregor | 6f4596c | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1607 | << R.get()->getSourceRange(); |
| 1608 | } else |
| 1609 | R = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1610 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1611 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1612 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1615 | return Actions.ActOnReturnStmt(ReturnLoc, R.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1618 | /// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled, |
| 1619 | /// this routine is called to collect the tokens for an MS asm statement. |
| 1620 | StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) { |
| 1621 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 1622 | SourceLocation EndLoc = AsmLoc; |
| 1623 | do { |
| 1624 | bool InBraces = false; |
NAKAMURA Takumi | 96e2171 | 2011-10-08 11:31:53 +0000 | [diff] [blame] | 1625 | unsigned short savedBraceCount = 0; |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1626 | bool InAsmComment = false; |
| 1627 | FileID FID; |
NAKAMURA Takumi | 96e2171 | 2011-10-08 11:31:53 +0000 | [diff] [blame] | 1628 | unsigned LineNo = 0; |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1629 | unsigned NumTokensRead = 0; |
| 1630 | SourceLocation LBraceLoc; |
| 1631 | |
| 1632 | if (Tok.is(tok::l_brace)) { |
| 1633 | // Braced inline asm: consume the opening brace. |
| 1634 | InBraces = true; |
| 1635 | savedBraceCount = BraceCount; |
| 1636 | EndLoc = LBraceLoc = ConsumeBrace(); |
| 1637 | ++NumTokensRead; |
| 1638 | } else { |
| 1639 | // Single-line inline asm; compute which line it is on. |
| 1640 | std::pair<FileID, unsigned> ExpAsmLoc = |
| 1641 | SrcMgr.getDecomposedExpansionLoc(EndLoc); |
| 1642 | FID = ExpAsmLoc.first; |
| 1643 | LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second); |
| 1644 | } |
| 1645 | |
Steve Naroff | 03d6bc6 | 2008-02-08 03:36:19 +0000 | [diff] [blame] | 1646 | SourceLocation TokLoc = Tok.getLocation(); |
Steve Naroff | 3628097 | 2008-02-08 18:01:27 +0000 | [diff] [blame] | 1647 | do { |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1648 | // If we hit EOF, we're done, period. |
| 1649 | if (Tok.is(tok::eof)) |
| 1650 | break; |
| 1651 | // When we consume the closing brace, we're done. |
| 1652 | if (InBraces && BraceCount == savedBraceCount) |
| 1653 | break; |
| 1654 | |
| 1655 | if (!InAsmComment && Tok.is(tok::semi)) { |
| 1656 | // A semicolon in an asm is the start of a comment. |
| 1657 | InAsmComment = true; |
| 1658 | if (InBraces) { |
| 1659 | // Compute which line the comment is on. |
| 1660 | std::pair<FileID, unsigned> ExpSemiLoc = |
| 1661 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
| 1662 | FID = ExpSemiLoc.first; |
| 1663 | LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second); |
| 1664 | } |
| 1665 | } else if (!InBraces || InAsmComment) { |
| 1666 | // If end-of-line is significant, check whether this token is on a |
| 1667 | // new line. |
| 1668 | std::pair<FileID, unsigned> ExpLoc = |
| 1669 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
| 1670 | if (ExpLoc.first != FID || |
| 1671 | SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) { |
| 1672 | // If this is a single-line __asm, we're done. |
| 1673 | if (!InBraces) |
| 1674 | break; |
| 1675 | // We're no longer in a comment. |
| 1676 | InAsmComment = false; |
| 1677 | } else if (!InAsmComment && Tok.is(tok::r_brace)) { |
| 1678 | // Single-line asm always ends when a closing brace is seen. |
| 1679 | // FIXME: This is compatible with Apple gcc's -fasm-blocks; what |
| 1680 | // does MSVC do here? |
| 1681 | break; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | // Consume the next token; make sure we don't modify the brace count etc. |
| 1686 | // if we are in a comment. |
Abramo Bagnara | a44724d | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1687 | EndLoc = TokLoc; |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1688 | if (InAsmComment) |
| 1689 | PP.Lex(Tok); |
| 1690 | else |
| 1691 | ConsumeAnyToken(); |
Steve Naroff | 3628097 | 2008-02-08 18:01:27 +0000 | [diff] [blame] | 1692 | TokLoc = Tok.getLocation(); |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1693 | ++NumTokensRead; |
| 1694 | } while (1); |
| 1695 | |
| 1696 | if (InBraces && BraceCount != savedBraceCount) { |
| 1697 | // __asm without closing brace (this can happen at EOF). |
| 1698 | Diag(Tok, diag::err_expected_rbrace); |
| 1699 | Diag(LBraceLoc, diag::note_matching) << "{"; |
| 1700 | return StmtError(); |
| 1701 | } else if (NumTokensRead == 0) { |
| 1702 | // Empty __asm. |
| 1703 | Diag(Tok, diag::err_expected_lbrace); |
| 1704 | return StmtError(); |
| 1705 | } |
| 1706 | // Multiple adjacent asm's form together into a single asm statement |
| 1707 | // in the AST. |
| 1708 | if (!Tok.is(tok::kw_asm)) |
| 1709 | break; |
| 1710 | EndLoc = ConsumeToken(); |
| 1711 | } while (1); |
| 1712 | // FIXME: Need to actually grab the data and pass it on to Sema. Ideally, |
| 1713 | // what Sema wants is a string of the entire inline asm, with one instruction |
| 1714 | // per line and all the __asm keywords stripped out, and a way of mapping |
| 1715 | // from any character of that string to its location in the original source |
| 1716 | // code. I'm not entirely sure how to go about that, though. |
Mike Stump | 95059b5 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1717 | Token t; |
| 1718 | t.setKind(tok::string_literal); |
Chris Lattner | e789685 | 2010-08-17 16:00:12 +0000 | [diff] [blame] | 1719 | t.setLiteralData("\"/*FIXME: not done*/\""); |
Mike Stump | 95059b5 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1720 | t.clearFlag(Token::NeedsCleaning); |
Chris Lattner | e789685 | 2010-08-17 16:00:12 +0000 | [diff] [blame] | 1721 | t.setLength(21); |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1722 | ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1)); |
Mike Stump | 95059b5 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1723 | ExprVector Constraints(Actions); |
| 1724 | ExprVector Exprs(Actions); |
| 1725 | ExprVector Clobbers(Actions); |
Abramo Bagnara | a44724d | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1726 | return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0, |
Mike Stump | 95059b5 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1727 | move_arg(Constraints), move_arg(Exprs), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1728 | AsmString.take(), move_arg(Clobbers), |
Abramo Bagnara | a44724d | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1729 | EndLoc, true); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1732 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1733 | /// asm-statement: |
| 1734 | /// gnu-asm-statement |
| 1735 | /// ms-asm-statement |
| 1736 | /// |
| 1737 | /// [GNU] gnu-asm-statement: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1738 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 1739 | /// |
| 1740 | /// [GNU] asm-argument: |
| 1741 | /// asm-string-literal |
| 1742 | /// asm-string-literal ':' asm-operands[opt] |
| 1743 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 1744 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 1745 | /// ':' asm-clobbers |
| 1746 | /// |
| 1747 | /// [GNU] asm-clobbers: |
| 1748 | /// asm-string-literal |
| 1749 | /// asm-clobbers ',' asm-string-literal |
| 1750 | /// |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1751 | /// [MS] ms-asm-statement: |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1752 | /// ms-asm-block |
| 1753 | /// ms-asm-block ms-asm-statement |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1754 | /// |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1755 | /// [MS] ms-asm-block: |
| 1756 | /// '__asm' ms-asm-line '\n' |
| 1757 | /// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt] |
| 1758 | /// |
| 1759 | /// [MS] ms-asm-instruction-block |
| 1760 | /// ms-asm-line |
| 1761 | /// ms-asm-line '\n' ms-asm-instruction-block |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1762 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1763 | StmtResult Parser::ParseAsmStatement(bool &msAsm) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1764 | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 1765 | SourceLocation AsmLoc = ConsumeToken(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1766 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1767 | if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) { |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1768 | msAsm = true; |
Eli Friedman | 3fedbe1 | 2011-09-30 01:13:51 +0000 | [diff] [blame] | 1769 | return ParseMicrosoftAsmStatement(AsmLoc); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1770 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1771 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | SourceLocation Loc = Tok.getLocation(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1773 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1775 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
| 1776 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1777 | Diag(Loc, diag::w_asm_qualifier_ignored) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1778 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1779 | Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict"; |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1780 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1781 | // Remember if this was a volatile asm. |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 1782 | bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1783 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1784 | Diag(Tok, diag::err_expected_lparen_after) << "asm"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1785 | SkipUntil(tok::r_paren); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1786 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1787 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1788 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1789 | T.consumeOpen(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1790 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1791 | ExprResult AsmString(ParseAsmStringLiteral()); |
Ted Kremenek | 320fa4b | 2011-12-02 01:30:14 +0000 | [diff] [blame] | 1792 | if (AsmString.isInvalid()) { |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 1793 | // Consume up to and including the closing paren. |
| 1794 | T.skipToEnd(); |
Sebastian Redl | 9a92034 | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1795 | return StmtError(); |
Ted Kremenek | 320fa4b | 2011-12-02 01:30:14 +0000 | [diff] [blame] | 1796 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1798 | SmallVector<IdentifierInfo *, 4> Names; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1799 | ExprVector Constraints(Actions); |
| 1800 | ExprVector Exprs(Actions); |
| 1801 | ExprVector Clobbers(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | |
Anders Carlsson | dfab34a | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 1803 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1804 | // We have a simple asm expression like 'asm("foo")'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1805 | T.consumeClose(); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1806 | return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1807 | /*NumOutputs*/ 0, /*NumInputs*/ 0, 0, |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1808 | move_arg(Constraints), move_arg(Exprs), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1809 | AsmString.take(), move_arg(Clobbers), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1810 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1811 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1812 | |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1813 | // Parse Outputs, if present. |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1814 | bool AteExtraColon = false; |
| 1815 | if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 1816 | // In C++ mode, parse "::" like ": :". |
| 1817 | AteExtraColon = Tok.is(tok::coloncolon); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1818 | ConsumeToken(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1820 | if (!AteExtraColon && |
| 1821 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1822 | return StmtError(); |
| 1823 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1825 | unsigned NumOutputs = Names.size(); |
| 1826 | |
| 1827 | // Parse Inputs, if present. |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1828 | if (AteExtraColon || |
| 1829 | Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 1830 | // In C++ mode, parse "::" like ": :". |
| 1831 | if (AteExtraColon) |
| 1832 | AteExtraColon = false; |
| 1833 | else { |
| 1834 | AteExtraColon = Tok.is(tok::coloncolon); |
| 1835 | ConsumeToken(); |
| 1836 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1837 | |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1838 | if (!AteExtraColon && |
| 1839 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1840 | return StmtError(); |
| 1841 | } |
| 1842 | |
| 1843 | assert(Names.size() == Constraints.size() && |
| 1844 | Constraints.size() == Exprs.size() && |
| 1845 | "Input operand size mismatch!"); |
| 1846 | |
| 1847 | unsigned NumInputs = Names.size() - NumOutputs; |
| 1848 | |
| 1849 | // Parse the clobbers, if present. |
Chris Lattner | 6405646 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1850 | if (AteExtraColon || Tok.is(tok::colon)) { |
| 1851 | if (!AteExtraColon) |
| 1852 | ConsumeToken(); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1853 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1854 | // Parse the asm-string list for clobbers if present. |
| 1855 | if (Tok.isNot(tok::r_paren)) { |
| 1856 | while (1) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1857 | ExprResult Clobber(ParseAsmStringLiteral()); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1858 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1859 | if (Clobber.isInvalid()) |
| 1860 | break; |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1861 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1862 | Clobbers.push_back(Clobber.release()); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1863 | |
Chandler Carruth | 102e1b6 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1864 | if (Tok.isNot(tok::comma)) break; |
| 1865 | ConsumeToken(); |
| 1866 | } |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1870 | T.consumeClose(); |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1871 | return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1872 | NumOutputs, NumInputs, Names.data(), |
Sebastian Redl | f512e82 | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 1873 | move_arg(Constraints), move_arg(Exprs), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1874 | AsmString.take(), move_arg(Clobbers), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1875 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | /// ParseAsmOperands - Parse the asm-operands production as used by |
Chris Lattner | 64cb475 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1879 | /// asm-statement, assuming the leading ':' token was eaten. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1880 | /// |
| 1881 | /// [GNU] asm-operands: |
| 1882 | /// asm-operand |
| 1883 | /// asm-operands ',' asm-operand |
| 1884 | /// |
| 1885 | /// [GNU] asm-operand: |
| 1886 | /// asm-string-literal '(' expression ')' |
| 1887 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 1888 | /// |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 1889 | // |
| 1890 | // FIXME: Avoid unnecessary std::string trashing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1891 | bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1892 | SmallVectorImpl<Expr *> &Constraints, |
| 1893 | SmallVectorImpl<Expr *> &Exprs) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1894 | // 'asm-operands' isn't present? |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1895 | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1896 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
| 1898 | while (1) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1899 | // Read the [id] if present. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1900 | if (Tok.is(tok::l_square)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1901 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 1902 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1904 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1905 | Diag(Tok, diag::err_expected_ident); |
| 1906 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1907 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1908 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1910 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 69efba7 | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 1911 | ConsumeToken(); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1912 | |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1913 | Names.push_back(II); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1914 | T.consumeClose(); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1915 | } else |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1916 | Names.push_back(0); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1917 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1918 | ExprResult Constraint(ParseAsmStringLiteral()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1919 | if (Constraint.isInvalid()) { |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1920 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1921 | return true; |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1922 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1923 | Constraints.push_back(Constraint.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1925 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1926 | Diag(Tok, diag::err_expected_lparen_after) << "asm operand"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1927 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1928 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1930 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1931 | // Read the parenthesized expression. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1932 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1933 | T.consumeOpen(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1934 | ExprResult Res(ParseExpression()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1935 | T.consumeClose(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1936 | if (Res.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1937 | SkipUntil(tok::r_paren); |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1938 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1940 | Exprs.push_back(Res.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1941 | // Eat the comma and continue parsing if it exists. |
Anders Carlsson | 8bd36fc | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1942 | if (Tok.isNot(tok::comma)) return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1943 | ConsumeToken(); |
| 1944 | } |
| 1945 | } |
Fariborz Jahanian | f9ed315 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1947 | Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) { |
Chris Lattner | 40e9bc8 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 1948 | assert(Tok.is(tok::l_brace)); |
| 1949 | SourceLocation LBraceLoc = Tok.getLocation(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1950 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1951 | if (SkipFunctionBodies && trySkippingFunctionBody()) { |
| 1952 | BodyScope.Exit(); |
| 1953 | return Actions.ActOnFinishFunctionBody(Decl, 0); |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1954 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1955 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1956 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc, |
| 1957 | "parsing function body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Fariborz Jahanian | f9ed315 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1959 | // Do not enter a scope for the brace, as the arguments are in the same scope |
| 1960 | // (the function body) as the body itself. Instead, just read the statement |
| 1961 | // list and put it into a CompoundStmt for safe keeping. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1962 | StmtResult FnBody(ParseCompoundStatementBody()); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1963 | |
Fariborz Jahanian | f9ed315 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1964 | // If the function body could not be parsed, make a bogus compoundstmt. |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1965 | if (FnBody.isInvalid()) { |
| 1966 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, |
Chris Lattner | 40e9bc8 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 1968 | MultiStmtArg(Actions), false); |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1969 | } |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1970 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1971 | BodyScope.Exit(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1972 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Seo Sanghyeon | cd5af4b | 2007-12-01 08:06:07 +0000 | [diff] [blame] | 1973 | } |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1974 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1975 | /// ParseFunctionTryBlock - Parse a C++ function-try-block. |
| 1976 | /// |
| 1977 | /// function-try-block: |
| 1978 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 1979 | /// |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1980 | Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) { |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1981 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 1982 | SourceLocation TryLoc = ConsumeToken(); |
| 1983 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1984 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc, |
| 1985 | "parsing function try block"); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1986 | |
| 1987 | // Constructor initializer list? |
| 1988 | if (Tok.is(tok::colon)) |
| 1989 | ParseConstructorInitializer(Decl); |
Douglas Gregor | 2eef427 | 2011-09-07 20:36:12 +0000 | [diff] [blame] | 1990 | else |
| 1991 | Actions.ActOnDefaultCtorInitializers(Decl); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1992 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1993 | if (SkipFunctionBodies && trySkippingFunctionBody()) { |
| 1994 | BodyScope.Exit(); |
| 1995 | return Actions.ActOnFinishFunctionBody(Decl, 0); |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 1996 | } |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 1997 | |
Sebastian Redl | de1b60a | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 1998 | SourceLocation LBraceLoc = Tok.getLocation(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1999 | StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc)); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2000 | // If we failed to parse the try-catch, we just give the function an empty |
| 2001 | // compound statement as the body. |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2002 | if (FnBody.isInvalid()) { |
| 2003 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Sebastian Redl | de1b60a | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 2004 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2005 | MultiStmtArg(Actions), false); |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2006 | } |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2008 | BodyScope.Exit(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2009 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2012 | bool Parser::trySkippingFunctionBody() { |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2013 | assert(Tok.is(tok::l_brace)); |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2014 | assert(SkipFunctionBodies && |
| 2015 | "Should only be called when SkipFunctionBodies is enabled"); |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2016 | |
| 2017 | // We're in code-completion mode. Skip parsing for all function bodies unless |
| 2018 | // the body contains the code-completion point. |
| 2019 | TentativeParsingAction PA(*this); |
| 2020 | ConsumeBrace(); |
| 2021 | if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false, |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2022 | /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) { |
Argyrios Kyrtzidis | 0fe5397 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2023 | PA.Commit(); |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
| 2027 | PA.Revert(); |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2031 | /// ParseCXXTryBlock - Parse a C++ try-block. |
| 2032 | /// |
| 2033 | /// try-block: |
| 2034 | /// 'try' compound-statement handler-seq |
| 2035 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2036 | StmtResult Parser::ParseCXXTryBlock() { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2037 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 2038 | |
| 2039 | SourceLocation TryLoc = ConsumeToken(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2040 | return ParseCXXTryBlockCommon(TryLoc); |
| 2041 | } |
| 2042 | |
| 2043 | /// ParseCXXTryBlockCommon - Parse the common part of try-block and |
| 2044 | /// function-try-block. |
| 2045 | /// |
| 2046 | /// try-block: |
| 2047 | /// 'try' compound-statement handler-seq |
| 2048 | /// |
| 2049 | /// function-try-block: |
| 2050 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 2051 | /// |
| 2052 | /// handler-seq: |
| 2053 | /// handler handler-seq[opt] |
| 2054 | /// |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2055 | /// [Borland] try-block: |
| 2056 | /// 'try' compound-statement seh-except-block |
| 2057 | /// 'try' compound-statment seh-finally-block |
| 2058 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2059 | StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2060 | if (Tok.isNot(tok::l_brace)) |
| 2061 | return StmtError(Diag(Tok, diag::err_expected_lbrace)); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2062 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2063 | |
| 2064 | StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2065 | Scope::DeclScope|Scope::TryScope)); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2066 | if (TryBlock.isInvalid()) |
| 2067 | return move(TryBlock); |
| 2068 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2069 | // Borland allows SEH-handlers with 'try' |
Douglas Gregor | b57791e | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 2070 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2071 | if ((Tok.is(tok::identifier) && |
| 2072 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) || |
| 2073 | Tok.is(tok::kw___finally)) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2074 | // TODO: Factor into common return ParseSEHHandlerCommon(...) |
| 2075 | StmtResult Handler; |
Douglas Gregor | b57791e | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 2076 | if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2077 | SourceLocation Loc = ConsumeToken(); |
| 2078 | Handler = ParseSEHExceptBlock(Loc); |
| 2079 | } |
| 2080 | else { |
| 2081 | SourceLocation Loc = ConsumeToken(); |
| 2082 | Handler = ParseSEHFinallyBlock(Loc); |
| 2083 | } |
| 2084 | if(Handler.isInvalid()) |
| 2085 | return move(Handler); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2086 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2087 | return Actions.ActOnSEHTryBlock(true /* IsCXXTry */, |
| 2088 | TryLoc, |
| 2089 | TryBlock.take(), |
| 2090 | Handler.take()); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2091 | } |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2092 | else { |
| 2093 | StmtVector Handlers(Actions); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2094 | ParsedAttributesWithRange attrs(AttrFactory); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2095 | MaybeParseCXX0XAttributes(attrs); |
| 2096 | ProhibitAttributes(attrs); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2097 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2098 | if (Tok.isNot(tok::kw_catch)) |
| 2099 | return StmtError(Diag(Tok, diag::err_expected_catch)); |
| 2100 | while (Tok.is(tok::kw_catch)) { |
| 2101 | StmtResult Handler(ParseCXXCatchBlock()); |
| 2102 | if (!Handler.isInvalid()) |
| 2103 | Handlers.push_back(Handler.release()); |
| 2104 | } |
| 2105 | // Don't bother creating the full statement if we don't have any usable |
| 2106 | // handlers. |
| 2107 | if (Handlers.empty()) |
| 2108 | return StmtError(); |
| 2109 | |
| 2110 | return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers)); |
| 2111 | } |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard |
| 2115 | /// |
| 2116 | /// handler: |
| 2117 | /// 'catch' '(' exception-declaration ')' compound-statement |
| 2118 | /// |
| 2119 | /// exception-declaration: |
| 2120 | /// type-specifier-seq declarator |
| 2121 | /// type-specifier-seq abstract-declarator |
| 2122 | /// type-specifier-seq |
| 2123 | /// '...' |
| 2124 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2125 | StmtResult Parser::ParseCXXCatchBlock() { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2126 | assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); |
| 2127 | |
| 2128 | SourceLocation CatchLoc = ConsumeToken(); |
| 2129 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2130 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2131 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2132 | return StmtError(); |
| 2133 | |
| 2134 | // C++ 3.3.2p3: |
| 2135 | // The name in a catch exception-declaration is local to the handler and |
| 2136 | // shall not be redeclared in the outermost block of the handler. |
| 2137 | ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope); |
| 2138 | |
| 2139 | // exception-declaration is equivalent to '...' or a parameter-declaration |
| 2140 | // without default arguments. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2141 | Decl *ExceptionDecl = 0; |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2142 | if (Tok.isNot(tok::ellipsis)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2143 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2144 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 2145 | return StmtError(); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2146 | Declarator ExDecl(DS, Declarator::CXXCatchContext); |
| 2147 | ParseDeclarator(ExDecl); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2148 | ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2149 | } else |
| 2150 | ConsumeToken(); |
| 2151 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2152 | T.consumeClose(); |
| 2153 | if (T.getCloseLocation().isInvalid()) |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2154 | return StmtError(); |
| 2155 | |
| 2156 | if (Tok.isNot(tok::l_brace)) |
| 2157 | return StmtError(Diag(Tok, diag::err_expected_lbrace)); |
| 2158 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2159 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2160 | StmtResult Block(ParseCompoundStatement()); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2161 | if (Block.isInvalid()) |
| 2162 | return move(Block); |
| 2163 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2164 | return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take()); |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2165 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2166 | |
| 2167 | void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) { |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2168 | IfExistsCondition Result; |
Francois Pichet | f986038 | 2011-05-07 17:30:27 +0000 | [diff] [blame] | 2169 | if (ParseMicrosoftIfExistsCondition(Result)) |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2170 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2172 | // Handle dependent statements by parsing the braces as a compound statement. |
| 2173 | // This is not the same behavior as Visual C++, which don't treat this as a |
| 2174 | // compound statement, but for Clang's type checking we can't have anything |
| 2175 | // inside these braces escaping to the surrounding code. |
| 2176 | if (Result.Behavior == IEB_Dependent) { |
| 2177 | if (!Tok.is(tok::l_brace)) { |
| 2178 | Diag(Tok, diag::err_expected_lbrace); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2179 | return; |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2180 | } |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2181 | |
| 2182 | StmtResult Compound = ParseCompoundStatement(); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2183 | if (Compound.isInvalid()) |
| 2184 | return; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2186 | StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc, |
| 2187 | Result.IsIfExists, |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2188 | Result.SS, |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2189 | Result.Name, |
| 2190 | Compound.get()); |
| 2191 | if (DepResult.isUsable()) |
| 2192 | Stmts.push_back(DepResult.get()); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2193 | return; |
| 2194 | } |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2196 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 2197 | if (Braces.consumeOpen()) { |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2198 | Diag(Tok, diag::err_expected_lbrace); |
| 2199 | return; |
| 2200 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2201 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2202 | switch (Result.Behavior) { |
| 2203 | case IEB_Parse: |
| 2204 | // Parse the statements below. |
| 2205 | break; |
| 2206 | |
| 2207 | case IEB_Dependent: |
| 2208 | llvm_unreachable("Dependent case handled above"); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2209 | |
| 2210 | case IEB_Skip: |
| 2211 | Braces.skipToEnd(); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2212 | return; |
| 2213 | } |
| 2214 | |
| 2215 | // Condition is true, parse the statements. |
| 2216 | while (Tok.isNot(tok::r_brace)) { |
| 2217 | StmtResult R = ParseStatementOrDeclaration(Stmts, false); |
| 2218 | if (R.isUsable()) |
| 2219 | Stmts.push_back(R.release()); |
| 2220 | } |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2221 | Braces.consumeClose(); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2222 | } |