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