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