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