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