blob: 87e2f343748e55bc57304005160e530dee349a2e [file] [log] [blame]
Argyrios Kyrtzidis4cc18a42008-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 Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000015#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
Francois Pichet8387e2a2011-04-22 22:18:13 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000019using namespace clang;
20
Sebastian Redld3a413d2009-04-26 20:35:05 +000021/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-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 McCalleee1d542011-02-14 07:13:47 +000024Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D,
Nico Weber48673472011-01-28 06:07:34 +000025 const ParsedTemplateInfo &TemplateInfo,
26 const VirtSpecifiers& VS) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000027 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sebastian Redld3a413d2009-04-26 20:35:05 +000028 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) &&
29 "Current token not a '{', ':' or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000030
John McCallf312b1e2010-08-26 23:41:50 +000031 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000032 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
34
John McCalld226f652010-08-21 09:40:31 +000035 Decl *FnD;
John McCall67d1a672009-08-06 02:15:43 +000036 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor37b372b2009-08-20 22:52:58 +000037 // FIXME: Friend templates
Douglas Gregor23c94db2010-07-02 17:43:08 +000038 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Sean Hunt4cd84942010-04-14 23:07:37 +000039 move(TemplateParams));
Nico Weber48673472011-01-28 06:07:34 +000040 else { // FIXME: pass template information through
Douglas Gregor23c94db2010-07-02 17:43:08 +000041 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000042 move(TemplateParams), 0,
Nico Weber48673472011-01-28 06:07:34 +000043 VS, 0, /*IsDefinition*/true);
44 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000045
Eli Friedmand33133c2009-07-22 21:45:50 +000046 HandleMemberFunctionDefaultArgs(D, FnD);
47
John McCalleee1d542011-02-14 07:13:47 +000048 D.complete(FnD);
49
Francois Pichet8387e2a2011-04-22 22:18:13 +000050 // In delayed template parsing mode, if we are within a class template
51 // or if we are about to parse function member template then consume
52 // the tokens and store them for parsing at the end of the translation unit.
53 if (getLang().DelayedTemplateParsing &&
54 ((Actions.CurContext->isDependentContext() ||
55 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
56 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
57 !D.getDeclSpec().isFriendSpecified()) {
58
59 if (FnD) {
60 LateParsedTemplatedFunction *LPT =
61 new LateParsedTemplatedFunction(this, FnD);
62
63 FunctionDecl *FD = 0;
64 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
65 FD = FunTmpl->getTemplatedDecl();
66 else
67 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +000068 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +000069
70 LateParsedTemplateMap[FD] = LPT;
71 Actions.MarkAsLateParsedTemplate(FD);
72 LexTemplateFunctionForLateParsing(LPT->Toks);
73 } else {
74 CachedTokens Toks;
75 LexTemplateFunctionForLateParsing(Toks);
76 }
77
78 return FnD;
79 }
80
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000081 // Consume the tokens and store them for later parsing.
82
Douglas Gregord54eb442010-10-12 16:25:54 +000083 LexedMethod* LM = new LexedMethod(this, FnD);
84 getCurrentClass().LateParsedDeclarations.push_back(LM);
85 LM->TemplateScope = getCurScope()->isTemplateParamScope();
86 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000087
Sebastian Redld3a413d2009-04-26 20:35:05 +000088 tok::TokenKind kind = Tok.getKind();
89 // We may have a constructor initializer or function-try-block here.
90 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000091 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000092 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000093 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000094 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000095 if (Tok.is(tok::semi)) {
96 // We found a semicolon; complain, consume the semicolon, and
97 // don't try to parse this method later.
98 Diag(Tok.getLocation(), diag::err_expected_lbrace);
99 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000100 delete getCurrentClass().LateParsedDeclarations.back();
101 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +0000102 return FnD;
103 }
104 }
105
Douglas Gregor7ad83902008-11-05 04:29:56 +0000106 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +0000108 Toks.push_back(Tok);
109 ConsumeBrace();
110 }
111 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000112 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000113
Sebastian Redld3a413d2009-04-26 20:35:05 +0000114 // If we're in a function-try-block, we need to store all the catch blocks.
115 if (kind == tok::kw_try) {
116 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000117 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
118 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000119 }
120 }
121
Douglas Gregor87f10642011-04-14 23:19:27 +0000122
123 if (!FnD) {
124 // If semantic analysis could not build a function declaration,
125 // just throw away the late-parsed declaration.
126 delete getCurrentClass().LateParsedDeclarations.back();
127 getCurrentClass().LateParsedDeclarations.pop_back();
128 }
129
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000130 return FnD;
131}
132
Douglas Gregord54eb442010-10-12 16:25:54 +0000133Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
134void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
135void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
136
137Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
138 : Self(P), Class(C) {}
139
140Parser::LateParsedClass::~LateParsedClass() {
141 Self->DeallocateParsedClasses(Class);
142}
143
144void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
145 Self->ParseLexedMethodDeclarations(*Class);
146}
147
148void Parser::LateParsedClass::ParseLexedMethodDefs() {
149 Self->ParseLexedMethodDefs(*Class);
150}
151
152void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
153 Self->ParseLexedMethodDeclaration(*this);
154}
155
156void Parser::LexedMethod::ParseLexedMethodDefs() {
157 Self->ParseLexedMethodDef(*this);
158}
159
Douglas Gregor72b505b2008-12-16 21:30:33 +0000160/// ParseLexedMethodDeclarations - We finished parsing the member
161/// specification of a top (non-nested) C++ class. Now go over the
162/// stack of method declarations with some parts for which parsing was
163/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000164void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
165 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000166 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000167 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000168 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000169
John McCall7a1dc562009-12-19 10:49:29 +0000170 // The current scope is still active if we're the top-level class.
171 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000172 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000173 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
174 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000175 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000176 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000177
Douglas Gregord54eb442010-10-12 16:25:54 +0000178 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
179 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000180 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000181
John McCall7a1dc562009-12-19 10:49:29 +0000182 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000183 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000184}
185
Douglas Gregord54eb442010-10-12 16:25:54 +0000186void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
187 // If this is a member template, introduce the template parameter scope.
188 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
189 if (LM.TemplateScope)
190 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
191
192 // Start the delayed C++ method declaration
193 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
194
195 // Introduce the parameters into scope and parse their default
196 // arguments.
197 ParseScope PrototypeScope(this,
198 Scope::FunctionPrototypeScope|Scope::DeclScope);
199 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
200 // Introduce the parameter into scope.
201 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
202
203 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
204 // Save the current token position.
205 SourceLocation origLoc = Tok.getLocation();
206
207 // Parse the default argument from its saved token stream.
208 Toks->push_back(Tok); // So that the current token doesn't get lost
209 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
210
211 // Consume the previously-pushed token.
212 ConsumeAnyToken();
213
214 // Consume the '='.
215 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
216 SourceLocation EqualLoc = ConsumeToken();
217
218 // The argument isn't actually potentially evaluated unless it is
219 // used.
220 EnterExpressionEvaluationContext Eval(Actions,
221 Sema::PotentiallyEvaluatedIfUsed);
222
223 ExprResult DefArgResult(ParseAssignmentExpression());
224 if (DefArgResult.isInvalid())
225 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
226 else {
227 if (Tok.is(tok::cxx_defaultarg_end))
228 ConsumeToken();
229 else
230 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
231 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
232 DefArgResult.take());
233 }
234
235 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
236 Tok.getLocation()) &&
237 "ParseAssignmentExpression went over the default arg tokens!");
238 // There could be leftover tokens (e.g. because of an error).
239 // Skip through until we reach the original token position.
240 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
241 ConsumeAnyToken();
242
243 delete Toks;
244 LM.DefaultArgs[I].Toks = 0;
245 }
246 }
247 PrototypeScope.Exit();
248
249 // Finish the delayed C++ method declaration.
250 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
251}
252
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000253/// ParseLexedMethodDefs - We finished parsing the member specification of a top
254/// (non-nested) C++ class. Now go over the stack of lexed methods that were
255/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000256void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
257 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000258 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000259 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000260 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000261
262 bool HasClassScope = !Class.TopLevelClass;
263 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
264 HasClassScope);
265
Douglas Gregord54eb442010-10-12 16:25:54 +0000266 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
267 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
268 }
269}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000270
Douglas Gregord54eb442010-10-12 16:25:54 +0000271void Parser::ParseLexedMethodDef(LexedMethod &LM) {
272 // If this is a member template, introduce the template parameter scope.
273 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
274 if (LM.TemplateScope)
275 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Douglas Gregord54eb442010-10-12 16:25:54 +0000277 // Save the current token position.
278 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000279
Douglas Gregord54eb442010-10-12 16:25:54 +0000280 assert(!LM.Toks.empty() && "Empty body!");
281 // Append the current token at the end of the new token stream so that it
282 // doesn't get lost.
283 LM.Toks.push_back(Tok);
284 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000285
Douglas Gregord54eb442010-10-12 16:25:54 +0000286 // Consume the previously pushed token.
287 ConsumeAnyToken();
288 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
289 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000290
Douglas Gregord54eb442010-10-12 16:25:54 +0000291 // Parse the method body. Function body parsing code is similar enough
292 // to be re-used for method bodies as well.
293 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
294 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000295
Douglas Gregord54eb442010-10-12 16:25:54 +0000296 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000297 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000298 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
299 Tok.getLocation()) &&
300 "ParseFunctionTryBlock went over the cached tokens!");
301 // There could be leftover tokens (e.g. because of an error).
302 // Skip through until we reach the original token position.
303 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
304 ConsumeAnyToken();
305 return;
306 }
307 if (Tok.is(tok::colon)) {
308 ParseConstructorInitializer(LM.D);
309
310 // Error recovery.
311 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000312 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000313 Actions.ActOnFinishFunctionBody(LM.D, 0);
314 return;
315 }
316 } else
317 Actions.ActOnDefaultCtorInitializers(LM.D);
318
Douglas Gregorc9977d02011-03-16 17:05:57 +0000319 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000320
321 if (Tok.getLocation() != origLoc) {
322 // Due to parsing error, we either went over the cached tokens or
323 // there are still cached tokens left. If it's the latter case skip the
324 // leftover tokens.
325 // Since this is an uncommon situation that should be avoided, use the
326 // expensive isBeforeInTranslationUnit call.
327 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
328 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000329 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000330 ConsumeAnyToken();
John McCalld6ca8da2010-04-10 07:37:23 +0000331
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000332 }
333}
334
335/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000336/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000337/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000338/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000339/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000340/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000341bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
342 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000343 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000344 // We always want this function to consume at least one token if the first
345 // token isn't T and if not at EOF.
346 bool isFirstTokenConsumed = true;
347 while (1) {
348 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000349 if (Tok.is(T1) || Tok.is(T2)) {
350 if (ConsumeFinalToken) {
351 Toks.push_back(Tok);
352 ConsumeAnyToken();
353 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000354 return true;
355 }
356
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000357 switch (Tok.getKind()) {
358 case tok::eof:
359 // Ran out of tokens.
360 return false;
361
362 case tok::l_paren:
363 // Recursively consume properly-nested parens.
364 Toks.push_back(Tok);
365 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000366 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000367 break;
368 case tok::l_square:
369 // Recursively consume properly-nested square brackets.
370 Toks.push_back(Tok);
371 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000372 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000373 break;
374 case tok::l_brace:
375 // Recursively consume properly-nested braces.
376 Toks.push_back(Tok);
377 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000378 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000379 break;
380
381 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
382 // Since the user wasn't looking for this token (if they were, it would
383 // already be handled), this isn't balanced. If there is a LHS token at a
384 // higher level, we will assume that this matches the unbalanced token
385 // and return it. Otherwise, this is a spurious RHS token, which we skip.
386 case tok::r_paren:
387 if (ParenCount && !isFirstTokenConsumed)
388 return false; // Matches something.
389 Toks.push_back(Tok);
390 ConsumeParen();
391 break;
392 case tok::r_square:
393 if (BracketCount && !isFirstTokenConsumed)
394 return false; // Matches something.
395 Toks.push_back(Tok);
396 ConsumeBracket();
397 break;
398 case tok::r_brace:
399 if (BraceCount && !isFirstTokenConsumed)
400 return false; // Matches something.
401 Toks.push_back(Tok);
402 ConsumeBrace();
403 break;
404
405 case tok::string_literal:
406 case tok::wide_string_literal:
407 Toks.push_back(Tok);
408 ConsumeStringToken();
409 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000410 case tok::semi:
411 if (StopAtSemi)
412 return false;
413 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000414 default:
415 // consume this token.
416 Toks.push_back(Tok);
417 ConsumeToken();
418 break;
419 }
420 isFirstTokenConsumed = false;
421 }
422}