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