blob: dee00275e7efd0eb6dbf96dc03489ba1ccaba600 [file] [log] [blame]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001//===--- 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 Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000015#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
Francois Pichet1c229c02011-04-22 22:18:13 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000019using namespace clang;
20
Sebastian Redla7b98a72009-04-26 20:35:05 +000021/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000022/// Declarator is a well formed C++ inline method definition. Now lex its body
23/// and store its tokens for parsing after the C++ class is complete.
John McCallc1465822011-02-14 07:13:47 +000024Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D,
Nico Weber24b2a822011-01-28 06:07:34 +000025 const ParsedTemplateInfo &TemplateInfo,
Francois Pichet3abc9b82011-05-11 02:14:46 +000026 const VirtSpecifiers& VS, ExprResult& Init) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +000027 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Alexis Hunt5a7fa252011-05-12 06:15:49 +000028 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
29 Tok.is(tok::equal)) &&
30 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000031
John McCallfaf5fb42010-08-26 23:41:50 +000032 MultiTemplateParamsArg TemplateParams(Actions,
Alexis Hunt1deb9722010-04-14 23:07:37 +000033 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
34 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
35
John McCall48871652010-08-21 09:40:31 +000036 Decl *FnD;
John McCall07e91c02009-08-06 02:15:43 +000037 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor3447e762009-08-20 22:52:58 +000038 // FIXME: Friend templates
Douglas Gregor0be31a22010-07-02 17:43:08 +000039 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Alexis Hunt1deb9722010-04-14 23:07:37 +000040 move(TemplateParams));
Nico Weber24b2a822011-01-28 06:07:34 +000041 else { // FIXME: pass template information through
Douglas Gregor0be31a22010-07-02 17:43:08 +000042 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlssondb36b802011-01-20 03:57:25 +000043 move(TemplateParams), 0,
Francois Pichet3abc9b82011-05-11 02:14:46 +000044 VS, Init.release(),
45 /*IsDefinition*/true);
Nico Weber24b2a822011-01-28 06:07:34 +000046 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000047
Eli Friedman3af2a772009-07-22 21:45:50 +000048 HandleMemberFunctionDefaultArgs(D, FnD);
49
John McCallc1465822011-02-14 07:13:47 +000050 D.complete(FnD);
51
Alexis Hunt5a7fa252011-05-12 06:15:49 +000052 if (Tok.is(tok::equal)) {
53 ConsumeToken();
54
55 bool Delete = false;
56 SourceLocation KWLoc;
57 if (Tok.is(tok::kw_delete)) {
58 if (!getLang().CPlusPlus0x)
59 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
60
61 KWLoc = ConsumeToken();
62 Actions.SetDeclDeleted(FnD, KWLoc);
63 Delete = true;
64 } else if (Tok.is(tok::kw_default)) {
65 if (!getLang().CPlusPlus0x)
66 Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
67
68 KWLoc = ConsumeToken();
69 Actions.SetDeclDefaulted(FnD, KWLoc);
70 } else {
71 llvm_unreachable("function definition after = not 'delete' or 'default'");
72 }
73
74 if (Tok.is(tok::comma)) {
75 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
76 << Delete;
77 SkipUntil(tok::semi);
78 } else {
79 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
80 Delete ? "delete" : "default", tok::semi);
81 }
82
83 return FnD;
84 }
85
Francois Pichet1c229c02011-04-22 22:18:13 +000086 // In delayed template parsing mode, if we are within a class template
87 // or if we are about to parse function member template then consume
88 // the tokens and store them for parsing at the end of the translation unit.
89 if (getLang().DelayedTemplateParsing &&
90 ((Actions.CurContext->isDependentContext() ||
91 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
92 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
93 !D.getDeclSpec().isFriendSpecified()) {
94
95 if (FnD) {
96 LateParsedTemplatedFunction *LPT =
97 new LateParsedTemplatedFunction(this, FnD);
98
99 FunctionDecl *FD = 0;
100 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
101 FD = FunTmpl->getTemplatedDecl();
102 else
103 FD = cast<FunctionDecl>(FnD);
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000104 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000105
106 LateParsedTemplateMap[FD] = LPT;
107 Actions.MarkAsLateParsedTemplate(FD);
108 LexTemplateFunctionForLateParsing(LPT->Toks);
109 } else {
110 CachedTokens Toks;
111 LexTemplateFunctionForLateParsing(Toks);
112 }
113
114 return FnD;
115 }
116
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000117 // Consume the tokens and store them for later parsing.
118
Douglas Gregorefc46952010-10-12 16:25:54 +0000119 LexedMethod* LM = new LexedMethod(this, FnD);
120 getCurrentClass().LateParsedDeclarations.push_back(LM);
121 LM->TemplateScope = getCurScope()->isTemplateParamScope();
122 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000123
Sebastian Redla7b98a72009-04-26 20:35:05 +0000124 tok::TokenKind kind = Tok.getKind();
125 // We may have a constructor initializer or function-try-block here.
126 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregore8381c02008-11-05 04:29:56 +0000127 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000128 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor49de5342008-11-10 16:59:40 +0000129 // We didn't find the left-brace we expected after the
Mike Stump11289f42009-09-09 15:08:12 +0000130 // constructor initializer.
Douglas Gregor49de5342008-11-10 16:59:40 +0000131 if (Tok.is(tok::semi)) {
132 // We found a semicolon; complain, consume the semicolon, and
133 // don't try to parse this method later.
134 Diag(Tok.getLocation(), diag::err_expected_lbrace);
135 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000136 delete getCurrentClass().LateParsedDeclarations.back();
137 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor49de5342008-11-10 16:59:40 +0000138 return FnD;
139 }
140 }
141
Douglas Gregore8381c02008-11-05 04:29:56 +0000142 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000143 // Begin by storing the '{' token.
Douglas Gregore8381c02008-11-05 04:29:56 +0000144 Toks.push_back(Tok);
145 ConsumeBrace();
146 }
147 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000148 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000149
Sebastian Redla7b98a72009-04-26 20:35:05 +0000150 // If we're in a function-try-block, we need to store all the catch blocks.
151 if (kind == tok::kw_try) {
152 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000153 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
154 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000155 }
156 }
157
Douglas Gregor6ca64102011-04-14 23:19:27 +0000158
159 if (!FnD) {
160 // If semantic analysis could not build a function declaration,
161 // just throw away the late-parsed declaration.
162 delete getCurrentClass().LateParsedDeclarations.back();
163 getCurrentClass().LateParsedDeclarations.pop_back();
164 }
165
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000166 return FnD;
167}
168
Douglas Gregorefc46952010-10-12 16:25:54 +0000169Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
170void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
171void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
172
173Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
174 : Self(P), Class(C) {}
175
176Parser::LateParsedClass::~LateParsedClass() {
177 Self->DeallocateParsedClasses(Class);
178}
179
180void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
181 Self->ParseLexedMethodDeclarations(*Class);
182}
183
184void Parser::LateParsedClass::ParseLexedMethodDefs() {
185 Self->ParseLexedMethodDefs(*Class);
186}
187
188void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
189 Self->ParseLexedMethodDeclaration(*this);
190}
191
192void Parser::LexedMethod::ParseLexedMethodDefs() {
193 Self->ParseLexedMethodDef(*this);
194}
195
Douglas Gregor4d87df52008-12-16 21:30:33 +0000196/// ParseLexedMethodDeclarations - We finished parsing the member
197/// specification of a top (non-nested) C++ class. Now go over the
198/// stack of method declarations with some parts for which parsing was
199/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000200void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
201 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000202 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000203 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000204 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000205
John McCall6df5fef2009-12-19 10:49:29 +0000206 // The current scope is still active if we're the top-level class.
207 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000208 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000209 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
210 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000211 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000212 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000213
Douglas Gregorefc46952010-10-12 16:25:54 +0000214 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
215 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000216 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000217
John McCall6df5fef2009-12-19 10:49:29 +0000218 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000219 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000220}
221
Douglas Gregorefc46952010-10-12 16:25:54 +0000222void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
223 // If this is a member template, introduce the template parameter scope.
224 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
225 if (LM.TemplateScope)
226 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
227
228 // Start the delayed C++ method declaration
229 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
230
231 // Introduce the parameters into scope and parse their default
232 // arguments.
233 ParseScope PrototypeScope(this,
234 Scope::FunctionPrototypeScope|Scope::DeclScope);
235 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
236 // Introduce the parameter into scope.
237 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
238
239 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
240 // Save the current token position.
241 SourceLocation origLoc = Tok.getLocation();
242
243 // Parse the default argument from its saved token stream.
244 Toks->push_back(Tok); // So that the current token doesn't get lost
245 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
246
247 // Consume the previously-pushed token.
248 ConsumeAnyToken();
249
250 // Consume the '='.
251 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
252 SourceLocation EqualLoc = ConsumeToken();
253
254 // The argument isn't actually potentially evaluated unless it is
255 // used.
256 EnterExpressionEvaluationContext Eval(Actions,
257 Sema::PotentiallyEvaluatedIfUsed);
258
259 ExprResult DefArgResult(ParseAssignmentExpression());
260 if (DefArgResult.isInvalid())
261 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
262 else {
263 if (Tok.is(tok::cxx_defaultarg_end))
264 ConsumeToken();
265 else
266 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
267 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
268 DefArgResult.take());
269 }
270
271 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
272 Tok.getLocation()) &&
273 "ParseAssignmentExpression went over the default arg tokens!");
274 // There could be leftover tokens (e.g. because of an error).
275 // Skip through until we reach the original token position.
276 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
277 ConsumeAnyToken();
278
279 delete Toks;
280 LM.DefaultArgs[I].Toks = 0;
281 }
282 }
283 PrototypeScope.Exit();
284
285 // Finish the delayed C++ method declaration.
286 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
287}
288
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000289/// ParseLexedMethodDefs - We finished parsing the member specification of a top
290/// (non-nested) C++ class. Now go over the stack of lexed methods that were
291/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000292void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
293 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000294 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000295 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000296 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000297
298 bool HasClassScope = !Class.TopLevelClass;
299 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
300 HasClassScope);
301
Douglas Gregorefc46952010-10-12 16:25:54 +0000302 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
303 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
304 }
305}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000306
Douglas Gregorefc46952010-10-12 16:25:54 +0000307void Parser::ParseLexedMethodDef(LexedMethod &LM) {
308 // If this is a member template, introduce the template parameter scope.
309 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
310 if (LM.TemplateScope)
311 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Douglas Gregorefc46952010-10-12 16:25:54 +0000313 // Save the current token position.
314 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000315
Douglas Gregorefc46952010-10-12 16:25:54 +0000316 assert(!LM.Toks.empty() && "Empty body!");
317 // Append the current token at the end of the new token stream so that it
318 // doesn't get lost.
319 LM.Toks.push_back(Tok);
320 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000321
Douglas Gregorefc46952010-10-12 16:25:54 +0000322 // Consume the previously pushed token.
323 ConsumeAnyToken();
324 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
325 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000326
Douglas Gregorefc46952010-10-12 16:25:54 +0000327 // Parse the method body. Function body parsing code is similar enough
328 // to be re-used for method bodies as well.
329 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
330 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000331
Douglas Gregorefc46952010-10-12 16:25:54 +0000332 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000333 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000334 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
335 Tok.getLocation()) &&
336 "ParseFunctionTryBlock went over the cached tokens!");
337 // There could be leftover tokens (e.g. because of an error).
338 // Skip through until we reach the original token position.
339 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
340 ConsumeAnyToken();
341 return;
342 }
343 if (Tok.is(tok::colon)) {
344 ParseConstructorInitializer(LM.D);
345
346 // Error recovery.
347 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000348 FnScope.Exit();
Douglas Gregorefc46952010-10-12 16:25:54 +0000349 Actions.ActOnFinishFunctionBody(LM.D, 0);
350 return;
351 }
352 } else
353 Actions.ActOnDefaultCtorInitializers(LM.D);
354
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000355 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000356
357 if (Tok.getLocation() != origLoc) {
358 // Due to parsing error, we either went over the cached tokens or
359 // there are still cached tokens left. If it's the latter case skip the
360 // leftover tokens.
361 // Since this is an uncommon situation that should be avoided, use the
362 // expensive isBeforeInTranslationUnit call.
363 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
364 origLoc))
Argyrios Kyrtzidise1224c82010-06-19 19:58:34 +0000365 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidise9b76af2010-06-17 10:52:22 +0000366 ConsumeAnyToken();
John McCallbb7b6582010-04-10 07:37:23 +0000367
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000368 }
369}
370
371/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000372/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000373/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000374/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000375/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000376/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000377bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
378 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000379 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000380 // We always want this function to consume at least one token if the first
381 // token isn't T and if not at EOF.
382 bool isFirstTokenConsumed = true;
383 while (1) {
384 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000385 if (Tok.is(T1) || Tok.is(T2)) {
386 if (ConsumeFinalToken) {
387 Toks.push_back(Tok);
388 ConsumeAnyToken();
389 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000390 return true;
391 }
392
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000393 switch (Tok.getKind()) {
394 case tok::eof:
395 // Ran out of tokens.
396 return false;
397
398 case tok::l_paren:
399 // Recursively consume properly-nested parens.
400 Toks.push_back(Tok);
401 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000402 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000403 break;
404 case tok::l_square:
405 // Recursively consume properly-nested square brackets.
406 Toks.push_back(Tok);
407 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000408 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000409 break;
410 case tok::l_brace:
411 // Recursively consume properly-nested braces.
412 Toks.push_back(Tok);
413 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000414 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000415 break;
416
417 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
418 // Since the user wasn't looking for this token (if they were, it would
419 // already be handled), this isn't balanced. If there is a LHS token at a
420 // higher level, we will assume that this matches the unbalanced token
421 // and return it. Otherwise, this is a spurious RHS token, which we skip.
422 case tok::r_paren:
423 if (ParenCount && !isFirstTokenConsumed)
424 return false; // Matches something.
425 Toks.push_back(Tok);
426 ConsumeParen();
427 break;
428 case tok::r_square:
429 if (BracketCount && !isFirstTokenConsumed)
430 return false; // Matches something.
431 Toks.push_back(Tok);
432 ConsumeBracket();
433 break;
434 case tok::r_brace:
435 if (BraceCount && !isFirstTokenConsumed)
436 return false; // Matches something.
437 Toks.push_back(Tok);
438 ConsumeBrace();
439 break;
440
441 case tok::string_literal:
442 case tok::wide_string_literal:
443 Toks.push_back(Tok);
444 ConsumeStringToken();
445 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000446 case tok::semi:
447 if (StopAtSemi)
448 return false;
449 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000450 default:
451 // consume this token.
452 Toks.push_back(Tok);
453 ConsumeToken();
454 break;
455 }
456 isFirstTokenConsumed = false;
457 }
458}