Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1 | //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements parsing for C++ class inline methods. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseDiagnostic.h" |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/DeclSpec.h" |
| 17 | #include "clang/Sema/Scope.h" |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 20 | /// ParseCXXInlineMethodDef - We parsed and verified that the specified |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 21 | /// Declarator is a well formed C++ inline method definition. Now lex its body |
| 22 | /// and store its tokens for parsing after the C++ class is complete. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 23 | Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D, |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 24 | const ParsedTemplateInfo &TemplateInfo, |
| 25 | const VirtSpecifiers& VS) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 26 | assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 27 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) && |
| 28 | "Current token not a '{', ':' or 'try'!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 29 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 30 | MultiTemplateParamsArg TemplateParams(Actions, |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 31 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, |
| 32 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); |
| 33 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 34 | Decl *FnD; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 35 | if (D.getDeclSpec().isFriendSpecified()) |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 36 | // FIXME: Friend templates |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 37 | FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true, |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 38 | move(TemplateParams)); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 39 | else { // FIXME: pass template information through |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 40 | FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 41 | move(TemplateParams), 0, |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 42 | VS, 0, /*IsDefinition*/true); |
| 43 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 44 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 45 | HandleMemberFunctionDefaultArgs(D, FnD); |
| 46 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 47 | D.complete(FnD); |
| 48 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 49 | // Consume the tokens and store them for later parsing. |
| 50 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 51 | LexedMethod* LM = new LexedMethod(this, FnD); |
| 52 | getCurrentClass().LateParsedDeclarations.push_back(LM); |
| 53 | LM->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 54 | CachedTokens &Toks = LM->Toks; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 55 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 56 | tok::TokenKind kind = Tok.getKind(); |
| 57 | // We may have a constructor initializer or function-try-block here. |
| 58 | if (kind == tok::colon || kind == tok::kw_try) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 59 | // Consume everything up to (and including) the left brace. |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 60 | if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) { |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 61 | // We didn't find the left-brace we expected after the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | // constructor initializer. |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 63 | if (Tok.is(tok::semi)) { |
| 64 | // We found a semicolon; complain, consume the semicolon, and |
| 65 | // don't try to parse this method later. |
| 66 | Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 67 | ConsumeAnyToken(); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 68 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 69 | getCurrentClass().LateParsedDeclarations.pop_back(); |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 70 | return FnD; |
| 71 | } |
| 72 | } |
| 73 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 74 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | // Begin by storing the '{' token. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 76 | Toks.push_back(Tok); |
| 77 | ConsumeBrace(); |
| 78 | } |
| 79 | // Consume everything up to (and including) the matching right brace. |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 80 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 81 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 82 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 83 | if (kind == tok::kw_try) { |
| 84 | while (Tok.is(tok::kw_catch)) { |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 85 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 86 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 90 | return FnD; |
| 91 | } |
| 92 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 93 | Parser::LateParsedDeclaration::~LateParsedDeclaration() {} |
| 94 | void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} |
| 95 | void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} |
| 96 | |
| 97 | Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) |
| 98 | : Self(P), Class(C) {} |
| 99 | |
| 100 | Parser::LateParsedClass::~LateParsedClass() { |
| 101 | Self->DeallocateParsedClasses(Class); |
| 102 | } |
| 103 | |
| 104 | void Parser::LateParsedClass::ParseLexedMethodDeclarations() { |
| 105 | Self->ParseLexedMethodDeclarations(*Class); |
| 106 | } |
| 107 | |
| 108 | void Parser::LateParsedClass::ParseLexedMethodDefs() { |
| 109 | Self->ParseLexedMethodDefs(*Class); |
| 110 | } |
| 111 | |
| 112 | void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { |
| 113 | Self->ParseLexedMethodDeclaration(*this); |
| 114 | } |
| 115 | |
| 116 | void Parser::LexedMethod::ParseLexedMethodDefs() { |
| 117 | Self->ParseLexedMethodDef(*this); |
| 118 | } |
| 119 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 120 | /// ParseLexedMethodDeclarations - We finished parsing the member |
| 121 | /// specification of a top (non-nested) C++ class. Now go over the |
| 122 | /// stack of method declarations with some parts for which parsing was |
| 123 | /// delayed (such as default arguments) and parse them. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 124 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 125 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 126 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 127 | if (HasTemplateScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 128 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 129 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 130 | // The current scope is still active if we're the top-level class. |
| 131 | // Otherwise we'll need to push and enter a new scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 132 | bool HasClassScope = !Class.TopLevelClass; |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 133 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 134 | HasClassScope); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 135 | if (HasClassScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 136 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 138 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 139 | Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 140 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 141 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 142 | if (HasClassScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 143 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 146 | void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { |
| 147 | // If this is a member template, introduce the template parameter scope. |
| 148 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 149 | if (LM.TemplateScope) |
| 150 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); |
| 151 | |
| 152 | // Start the delayed C++ method declaration |
| 153 | Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 154 | |
| 155 | // Introduce the parameters into scope and parse their default |
| 156 | // arguments. |
| 157 | ParseScope PrototypeScope(this, |
| 158 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
| 159 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 160 | // Introduce the parameter into scope. |
| 161 | Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param); |
| 162 | |
| 163 | if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { |
| 164 | // Save the current token position. |
| 165 | SourceLocation origLoc = Tok.getLocation(); |
| 166 | |
| 167 | // Parse the default argument from its saved token stream. |
| 168 | Toks->push_back(Tok); // So that the current token doesn't get lost |
| 169 | PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); |
| 170 | |
| 171 | // Consume the previously-pushed token. |
| 172 | ConsumeAnyToken(); |
| 173 | |
| 174 | // Consume the '='. |
| 175 | assert(Tok.is(tok::equal) && "Default argument not starting with '='"); |
| 176 | SourceLocation EqualLoc = ConsumeToken(); |
| 177 | |
| 178 | // The argument isn't actually potentially evaluated unless it is |
| 179 | // used. |
| 180 | EnterExpressionEvaluationContext Eval(Actions, |
| 181 | Sema::PotentiallyEvaluatedIfUsed); |
| 182 | |
| 183 | ExprResult DefArgResult(ParseAssignmentExpression()); |
| 184 | if (DefArgResult.isInvalid()) |
| 185 | Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); |
| 186 | else { |
| 187 | if (Tok.is(tok::cxx_defaultarg_end)) |
| 188 | ConsumeToken(); |
| 189 | else |
| 190 | Diag(Tok.getLocation(), diag::err_default_arg_unparsed); |
| 191 | Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, |
| 192 | DefArgResult.take()); |
| 193 | } |
| 194 | |
| 195 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 196 | Tok.getLocation()) && |
| 197 | "ParseAssignmentExpression went over the default arg tokens!"); |
| 198 | // There could be leftover tokens (e.g. because of an error). |
| 199 | // Skip through until we reach the original token position. |
| 200 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 201 | ConsumeAnyToken(); |
| 202 | |
| 203 | delete Toks; |
| 204 | LM.DefaultArgs[I].Toks = 0; |
| 205 | } |
| 206 | } |
| 207 | PrototypeScope.Exit(); |
| 208 | |
| 209 | // Finish the delayed C++ method declaration. |
| 210 | Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 211 | } |
| 212 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 213 | /// ParseLexedMethodDefs - We finished parsing the member specification of a top |
| 214 | /// (non-nested) C++ class. Now go over the stack of lexed methods that were |
| 215 | /// collected during its parsing and parse them all. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 216 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 217 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 218 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 219 | if (HasTemplateScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 220 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 221 | |
| 222 | bool HasClassScope = !Class.TopLevelClass; |
| 223 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 224 | HasClassScope); |
| 225 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 226 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 227 | Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); |
| 228 | } |
| 229 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 231 | void Parser::ParseLexedMethodDef(LexedMethod &LM) { |
| 232 | // If this is a member template, introduce the template parameter scope. |
| 233 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 234 | if (LM.TemplateScope) |
| 235 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 237 | // Save the current token position. |
| 238 | SourceLocation origLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | c50a5e0 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 239 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 240 | assert(!LM.Toks.empty() && "Empty body!"); |
| 241 | // Append the current token at the end of the new token stream so that it |
| 242 | // doesn't get lost. |
| 243 | LM.Toks.push_back(Tok); |
| 244 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 246 | // Consume the previously pushed token. |
| 247 | ConsumeAnyToken(); |
| 248 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) |
| 249 | && "Inline method not starting with '{', ':' or 'try'"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 251 | // Parse the method body. Function body parsing code is similar enough |
| 252 | // to be re-used for method bodies as well. |
| 253 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
| 254 | Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 256 | if (Tok.is(tok::kw_try)) { |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 257 | ParseFunctionTryBlock(LM.D, FnScope); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 258 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 259 | Tok.getLocation()) && |
| 260 | "ParseFunctionTryBlock went over the cached tokens!"); |
| 261 | // There could be leftover tokens (e.g. because of an error). |
| 262 | // Skip through until we reach the original token position. |
| 263 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 264 | ConsumeAnyToken(); |
| 265 | return; |
| 266 | } |
| 267 | if (Tok.is(tok::colon)) { |
| 268 | ParseConstructorInitializer(LM.D); |
| 269 | |
| 270 | // Error recovery. |
| 271 | if (!Tok.is(tok::l_brace)) { |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 272 | FnScope.Exit(); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 273 | Actions.ActOnFinishFunctionBody(LM.D, 0); |
| 274 | return; |
| 275 | } |
| 276 | } else |
| 277 | Actions.ActOnDefaultCtorInitializers(LM.D); |
| 278 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 279 | ParseFunctionStatementBody(LM.D, FnScope); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 280 | |
| 281 | if (Tok.getLocation() != origLoc) { |
| 282 | // Due to parsing error, we either went over the cached tokens or |
| 283 | // there are still cached tokens left. If it's the latter case skip the |
| 284 | // leftover tokens. |
| 285 | // Since this is an uncommon situation that should be avoided, use the |
| 286 | // expensive isBeforeInTranslationUnit call. |
| 287 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 288 | origLoc)) |
Argyrios Kyrtzidis | 8f9359f | 2010-06-19 19:58:34 +0000 | [diff] [blame] | 289 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | 7558cd0 | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 290 | ConsumeAnyToken(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 291 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 296 | /// container until the token 'T' is reached (which gets |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | /// consumed/stored too, if ConsumeFinalToken). |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 298 | /// If StopAtSemi is true, then we will stop early at a ';' character. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 299 | /// Returns true if token 'T1' or 'T2' was found. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 300 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 301 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 302 | CachedTokens &Toks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 303 | bool StopAtSemi, bool ConsumeFinalToken) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 304 | // We always want this function to consume at least one token if the first |
| 305 | // token isn't T and if not at EOF. |
| 306 | bool isFirstTokenConsumed = true; |
| 307 | while (1) { |
| 308 | // If we found one of the tokens, stop and return true. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 309 | if (Tok.is(T1) || Tok.is(T2)) { |
| 310 | if (ConsumeFinalToken) { |
| 311 | Toks.push_back(Tok); |
| 312 | ConsumeAnyToken(); |
| 313 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 317 | switch (Tok.getKind()) { |
| 318 | case tok::eof: |
| 319 | // Ran out of tokens. |
| 320 | return false; |
| 321 | |
| 322 | case tok::l_paren: |
| 323 | // Recursively consume properly-nested parens. |
| 324 | Toks.push_back(Tok); |
| 325 | ConsumeParen(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 326 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 327 | break; |
| 328 | case tok::l_square: |
| 329 | // Recursively consume properly-nested square brackets. |
| 330 | Toks.push_back(Tok); |
| 331 | ConsumeBracket(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 332 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 333 | break; |
| 334 | case tok::l_brace: |
| 335 | // Recursively consume properly-nested braces. |
| 336 | Toks.push_back(Tok); |
| 337 | ConsumeBrace(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 338 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 339 | break; |
| 340 | |
| 341 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 342 | // Since the user wasn't looking for this token (if they were, it would |
| 343 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 344 | // higher level, we will assume that this matches the unbalanced token |
| 345 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 346 | case tok::r_paren: |
| 347 | if (ParenCount && !isFirstTokenConsumed) |
| 348 | return false; // Matches something. |
| 349 | Toks.push_back(Tok); |
| 350 | ConsumeParen(); |
| 351 | break; |
| 352 | case tok::r_square: |
| 353 | if (BracketCount && !isFirstTokenConsumed) |
| 354 | return false; // Matches something. |
| 355 | Toks.push_back(Tok); |
| 356 | ConsumeBracket(); |
| 357 | break; |
| 358 | case tok::r_brace: |
| 359 | if (BraceCount && !isFirstTokenConsumed) |
| 360 | return false; // Matches something. |
| 361 | Toks.push_back(Tok); |
| 362 | ConsumeBrace(); |
| 363 | break; |
| 364 | |
| 365 | case tok::string_literal: |
| 366 | case tok::wide_string_literal: |
| 367 | Toks.push_back(Tok); |
| 368 | ConsumeStringToken(); |
| 369 | break; |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 370 | case tok::semi: |
| 371 | if (StopAtSemi) |
| 372 | return false; |
| 373 | // FALL THROUGH. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 374 | default: |
| 375 | // consume this token. |
| 376 | Toks.push_back(Tok); |
| 377 | ConsumeToken(); |
| 378 | break; |
| 379 | } |
| 380 | isFirstTokenConsumed = false; |
| 381 | } |
| 382 | } |