blob: 778aa11087229e56ef82b68f46091348bf9eb5d5 [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,
26 const VirtSpecifiers& VS) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +000027 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sebastian Redla7b98a72009-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 Kyrtzidis7bbb20e2008-06-24 22:12:16 +000030
John McCallfaf5fb42010-08-26 23:41:50 +000031 MultiTemplateParamsArg TemplateParams(Actions,
Alexis Hunt1deb9722010-04-14 23:07:37 +000032 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
34
John McCall48871652010-08-21 09:40:31 +000035 Decl *FnD;
John McCall07e91c02009-08-06 02:15:43 +000036 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor3447e762009-08-20 22:52:58 +000037 // FIXME: Friend templates
Douglas Gregor0be31a22010-07-02 17:43:08 +000038 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Alexis Hunt1deb9722010-04-14 23:07:37 +000039 move(TemplateParams));
Nico Weber24b2a822011-01-28 06:07:34 +000040 else { // FIXME: pass template information through
Douglas Gregor0be31a22010-07-02 17:43:08 +000041 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlssondb36b802011-01-20 03:57:25 +000042 move(TemplateParams), 0,
Nico Weber24b2a822011-01-28 06:07:34 +000043 VS, 0, /*IsDefinition*/true);
44 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000045
Eli Friedman3af2a772009-07-22 21:45:50 +000046 HandleMemberFunctionDefaultArgs(D, FnD);
47
John McCallc1465822011-02-14 07:13:47 +000048 D.complete(FnD);
49
Francois Pichet1c229c02011-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);
68
69 LateParsedTemplateMap[FD] = LPT;
70 Actions.MarkAsLateParsedTemplate(FD);
71 LexTemplateFunctionForLateParsing(LPT->Toks);
72 } else {
73 CachedTokens Toks;
74 LexTemplateFunctionForLateParsing(Toks);
75 }
76
77 return FnD;
78 }
79
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000080 // Consume the tokens and store them for later parsing.
81
Douglas Gregorefc46952010-10-12 16:25:54 +000082 LexedMethod* LM = new LexedMethod(this, FnD);
83 getCurrentClass().LateParsedDeclarations.push_back(LM);
84 LM->TemplateScope = getCurScope()->isTemplateParamScope();
85 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000086
Sebastian Redla7b98a72009-04-26 20:35:05 +000087 tok::TokenKind kind = Tok.getKind();
88 // We may have a constructor initializer or function-try-block here.
89 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregore8381c02008-11-05 04:29:56 +000090 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +000091 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor49de5342008-11-10 16:59:40 +000092 // We didn't find the left-brace we expected after the
Mike Stump11289f42009-09-09 15:08:12 +000093 // constructor initializer.
Douglas Gregor49de5342008-11-10 16:59:40 +000094 if (Tok.is(tok::semi)) {
95 // We found a semicolon; complain, consume the semicolon, and
96 // don't try to parse this method later.
97 Diag(Tok.getLocation(), diag::err_expected_lbrace);
98 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +000099 delete getCurrentClass().LateParsedDeclarations.back();
100 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor49de5342008-11-10 16:59:40 +0000101 return FnD;
102 }
103 }
104
Douglas Gregore8381c02008-11-05 04:29:56 +0000105 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000106 // Begin by storing the '{' token.
Douglas Gregore8381c02008-11-05 04:29:56 +0000107 Toks.push_back(Tok);
108 ConsumeBrace();
109 }
110 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000111 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000112
Sebastian Redla7b98a72009-04-26 20:35:05 +0000113 // If we're in a function-try-block, we need to store all the catch blocks.
114 if (kind == tok::kw_try) {
115 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000116 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
117 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000118 }
119 }
120
Douglas Gregor6ca64102011-04-14 23:19:27 +0000121
122 if (!FnD) {
123 // If semantic analysis could not build a function declaration,
124 // just throw away the late-parsed declaration.
125 delete getCurrentClass().LateParsedDeclarations.back();
126 getCurrentClass().LateParsedDeclarations.pop_back();
127 }
128
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000129 return FnD;
130}
131
Douglas Gregorefc46952010-10-12 16:25:54 +0000132Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
133void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
134void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
135
136Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
137 : Self(P), Class(C) {}
138
139Parser::LateParsedClass::~LateParsedClass() {
140 Self->DeallocateParsedClasses(Class);
141}
142
143void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
144 Self->ParseLexedMethodDeclarations(*Class);
145}
146
147void Parser::LateParsedClass::ParseLexedMethodDefs() {
148 Self->ParseLexedMethodDefs(*Class);
149}
150
151void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
152 Self->ParseLexedMethodDeclaration(*this);
153}
154
155void Parser::LexedMethod::ParseLexedMethodDefs() {
156 Self->ParseLexedMethodDef(*this);
157}
158
Douglas Gregor4d87df52008-12-16 21:30:33 +0000159/// ParseLexedMethodDeclarations - We finished parsing the member
160/// specification of a top (non-nested) C++ class. Now go over the
161/// stack of method declarations with some parts for which parsing was
162/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000163void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
164 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000165 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000166 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000167 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000168
John McCall6df5fef2009-12-19 10:49:29 +0000169 // The current scope is still active if we're the top-level class.
170 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000171 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000172 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
173 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000174 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000175 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000176
Douglas Gregorefc46952010-10-12 16:25:54 +0000177 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
178 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000179 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000180
John McCall6df5fef2009-12-19 10:49:29 +0000181 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000182 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000183}
184
Douglas Gregorefc46952010-10-12 16:25:54 +0000185void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
186 // If this is a member template, introduce the template parameter scope.
187 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
188 if (LM.TemplateScope)
189 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
190
191 // Start the delayed C++ method declaration
192 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
193
194 // Introduce the parameters into scope and parse their default
195 // arguments.
196 ParseScope PrototypeScope(this,
197 Scope::FunctionPrototypeScope|Scope::DeclScope);
198 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
199 // Introduce the parameter into scope.
200 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
201
202 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
203 // Save the current token position.
204 SourceLocation origLoc = Tok.getLocation();
205
206 // Parse the default argument from its saved token stream.
207 Toks->push_back(Tok); // So that the current token doesn't get lost
208 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
209
210 // Consume the previously-pushed token.
211 ConsumeAnyToken();
212
213 // Consume the '='.
214 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
215 SourceLocation EqualLoc = ConsumeToken();
216
217 // The argument isn't actually potentially evaluated unless it is
218 // used.
219 EnterExpressionEvaluationContext Eval(Actions,
220 Sema::PotentiallyEvaluatedIfUsed);
221
222 ExprResult DefArgResult(ParseAssignmentExpression());
223 if (DefArgResult.isInvalid())
224 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
225 else {
226 if (Tok.is(tok::cxx_defaultarg_end))
227 ConsumeToken();
228 else
229 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
230 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
231 DefArgResult.take());
232 }
233
234 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
235 Tok.getLocation()) &&
236 "ParseAssignmentExpression went over the default arg tokens!");
237 // There could be leftover tokens (e.g. because of an error).
238 // Skip through until we reach the original token position.
239 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
240 ConsumeAnyToken();
241
242 delete Toks;
243 LM.DefaultArgs[I].Toks = 0;
244 }
245 }
246 PrototypeScope.Exit();
247
248 // Finish the delayed C++ method declaration.
249 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
250}
251
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000252/// ParseLexedMethodDefs - We finished parsing the member specification of a top
253/// (non-nested) C++ class. Now go over the stack of lexed methods that were
254/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000255void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
256 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000257 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000258 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000259 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000260
261 bool HasClassScope = !Class.TopLevelClass;
262 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
263 HasClassScope);
264
Douglas Gregorefc46952010-10-12 16:25:54 +0000265 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
266 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
267 }
268}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000269
Douglas Gregorefc46952010-10-12 16:25:54 +0000270void Parser::ParseLexedMethodDef(LexedMethod &LM) {
271 // If this is a member template, introduce the template parameter scope.
272 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
273 if (LM.TemplateScope)
274 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump11289f42009-09-09 15:08:12 +0000275
Douglas Gregorefc46952010-10-12 16:25:54 +0000276 // Save the current token position.
277 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000278
Douglas Gregorefc46952010-10-12 16:25:54 +0000279 assert(!LM.Toks.empty() && "Empty body!");
280 // Append the current token at the end of the new token stream so that it
281 // doesn't get lost.
282 LM.Toks.push_back(Tok);
283 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000284
Douglas Gregorefc46952010-10-12 16:25:54 +0000285 // Consume the previously pushed token.
286 ConsumeAnyToken();
287 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
288 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000289
Douglas Gregorefc46952010-10-12 16:25:54 +0000290 // Parse the method body. Function body parsing code is similar enough
291 // to be re-used for method bodies as well.
292 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
293 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000294
Douglas Gregorefc46952010-10-12 16:25:54 +0000295 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000296 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000297 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
298 Tok.getLocation()) &&
299 "ParseFunctionTryBlock went over the cached tokens!");
300 // There could be leftover tokens (e.g. because of an error).
301 // Skip through until we reach the original token position.
302 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
303 ConsumeAnyToken();
304 return;
305 }
306 if (Tok.is(tok::colon)) {
307 ParseConstructorInitializer(LM.D);
308
309 // Error recovery.
310 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000311 FnScope.Exit();
Douglas Gregorefc46952010-10-12 16:25:54 +0000312 Actions.ActOnFinishFunctionBody(LM.D, 0);
313 return;
314 }
315 } else
316 Actions.ActOnDefaultCtorInitializers(LM.D);
317
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000318 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000319
320 if (Tok.getLocation() != origLoc) {
321 // Due to parsing error, we either went over the cached tokens or
322 // there are still cached tokens left. If it's the latter case skip the
323 // leftover tokens.
324 // Since this is an uncommon situation that should be avoided, use the
325 // expensive isBeforeInTranslationUnit call.
326 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
327 origLoc))
Argyrios Kyrtzidise1224c82010-06-19 19:58:34 +0000328 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidise9b76af2010-06-17 10:52:22 +0000329 ConsumeAnyToken();
John McCallbb7b6582010-04-10 07:37:23 +0000330
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000331 }
332}
333
334/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000335/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000336/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000337/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000338/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000339/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000340bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
341 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000342 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000343 // We always want this function to consume at least one token if the first
344 // token isn't T and if not at EOF.
345 bool isFirstTokenConsumed = true;
346 while (1) {
347 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000348 if (Tok.is(T1) || Tok.is(T2)) {
349 if (ConsumeFinalToken) {
350 Toks.push_back(Tok);
351 ConsumeAnyToken();
352 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000353 return true;
354 }
355
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000356 switch (Tok.getKind()) {
357 case tok::eof:
358 // Ran out of tokens.
359 return false;
360
361 case tok::l_paren:
362 // Recursively consume properly-nested parens.
363 Toks.push_back(Tok);
364 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000365 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000366 break;
367 case tok::l_square:
368 // Recursively consume properly-nested square brackets.
369 Toks.push_back(Tok);
370 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000371 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000372 break;
373 case tok::l_brace:
374 // Recursively consume properly-nested braces.
375 Toks.push_back(Tok);
376 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000377 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000378 break;
379
380 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
381 // Since the user wasn't looking for this token (if they were, it would
382 // already be handled), this isn't balanced. If there is a LHS token at a
383 // higher level, we will assume that this matches the unbalanced token
384 // and return it. Otherwise, this is a spurious RHS token, which we skip.
385 case tok::r_paren:
386 if (ParenCount && !isFirstTokenConsumed)
387 return false; // Matches something.
388 Toks.push_back(Tok);
389 ConsumeParen();
390 break;
391 case tok::r_square:
392 if (BracketCount && !isFirstTokenConsumed)
393 return false; // Matches something.
394 Toks.push_back(Tok);
395 ConsumeBracket();
396 break;
397 case tok::r_brace:
398 if (BraceCount && !isFirstTokenConsumed)
399 return false; // Matches something.
400 Toks.push_back(Tok);
401 ConsumeBrace();
402 break;
403
404 case tok::string_literal:
405 case tok::wide_string_literal:
406 Toks.push_back(Tok);
407 ConsumeStringToken();
408 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000409 case tok::semi:
410 if (StopAtSemi)
411 return false;
412 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000413 default:
414 // consume this token.
415 Toks.push_back(Tok);
416 ConsumeToken();
417 break;
418 }
419 isFirstTokenConsumed = false;
420 }
421}