blob: 1e1a0e2b0700cde55f808f3c46d317b623d9bd39 [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,
Francois Pichet6a247472011-05-11 02:14:46 +000026 const VirtSpecifiers& VS, ExprResult& Init) {
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,
Francois Pichet6a247472011-05-11 02:14:46 +000043 VS, Init.release(),
44 /*IsDefinition*/true);
Nico Weber48673472011-01-28 06:07:34 +000045 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000046
Eli Friedmand33133c2009-07-22 21:45:50 +000047 HandleMemberFunctionDefaultArgs(D, FnD);
48
John McCalleee1d542011-02-14 07:13:47 +000049 D.complete(FnD);
50
Francois Pichet8387e2a2011-04-22 22:18:13 +000051 // In delayed template parsing mode, if we are within a class template
52 // or if we are about to parse function member template then consume
53 // the tokens and store them for parsing at the end of the translation unit.
54 if (getLang().DelayedTemplateParsing &&
55 ((Actions.CurContext->isDependentContext() ||
56 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
57 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
58 !D.getDeclSpec().isFriendSpecified()) {
59
60 if (FnD) {
61 LateParsedTemplatedFunction *LPT =
62 new LateParsedTemplatedFunction(this, FnD);
63
64 FunctionDecl *FD = 0;
65 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
66 FD = FunTmpl->getTemplatedDecl();
67 else
68 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +000069 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +000070
71 LateParsedTemplateMap[FD] = LPT;
72 Actions.MarkAsLateParsedTemplate(FD);
73 LexTemplateFunctionForLateParsing(LPT->Toks);
74 } else {
75 CachedTokens Toks;
76 LexTemplateFunctionForLateParsing(Toks);
77 }
78
79 return FnD;
80 }
81
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000082 // Consume the tokens and store them for later parsing.
83
Douglas Gregord54eb442010-10-12 16:25:54 +000084 LexedMethod* LM = new LexedMethod(this, FnD);
85 getCurrentClass().LateParsedDeclarations.push_back(LM);
86 LM->TemplateScope = getCurScope()->isTemplateParamScope();
87 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000088
Sebastian Redld3a413d2009-04-26 20:35:05 +000089 tok::TokenKind kind = Tok.getKind();
90 // We may have a constructor initializer or function-try-block here.
91 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000092 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000093 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000094 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000095 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000096 if (Tok.is(tok::semi)) {
97 // We found a semicolon; complain, consume the semicolon, and
98 // don't try to parse this method later.
99 Diag(Tok.getLocation(), diag::err_expected_lbrace);
100 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000101 delete getCurrentClass().LateParsedDeclarations.back();
102 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +0000103 return FnD;
104 }
105 }
106
Douglas Gregor7ad83902008-11-05 04:29:56 +0000107 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +0000109 Toks.push_back(Tok);
110 ConsumeBrace();
111 }
112 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000113 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000114
Sebastian Redld3a413d2009-04-26 20:35:05 +0000115 // If we're in a function-try-block, we need to store all the catch blocks.
116 if (kind == tok::kw_try) {
117 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000118 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
119 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000120 }
121 }
122
Douglas Gregor87f10642011-04-14 23:19:27 +0000123
124 if (!FnD) {
125 // If semantic analysis could not build a function declaration,
126 // just throw away the late-parsed declaration.
127 delete getCurrentClass().LateParsedDeclarations.back();
128 getCurrentClass().LateParsedDeclarations.pop_back();
129 }
130
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000131 return FnD;
132}
133
Douglas Gregord54eb442010-10-12 16:25:54 +0000134Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
135void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
136void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
137
138Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
139 : Self(P), Class(C) {}
140
141Parser::LateParsedClass::~LateParsedClass() {
142 Self->DeallocateParsedClasses(Class);
143}
144
145void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
146 Self->ParseLexedMethodDeclarations(*Class);
147}
148
149void Parser::LateParsedClass::ParseLexedMethodDefs() {
150 Self->ParseLexedMethodDefs(*Class);
151}
152
153void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
154 Self->ParseLexedMethodDeclaration(*this);
155}
156
157void Parser::LexedMethod::ParseLexedMethodDefs() {
158 Self->ParseLexedMethodDef(*this);
159}
160
Douglas Gregor72b505b2008-12-16 21:30:33 +0000161/// ParseLexedMethodDeclarations - We finished parsing the member
162/// specification of a top (non-nested) C++ class. Now go over the
163/// stack of method declarations with some parts for which parsing was
164/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000165void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
166 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000167 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000168 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000169 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000170
John McCall7a1dc562009-12-19 10:49:29 +0000171 // The current scope is still active if we're the top-level class.
172 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000173 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000174 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
175 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000176 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000177 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000178
Douglas Gregord54eb442010-10-12 16:25:54 +0000179 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
180 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000181 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000182
John McCall7a1dc562009-12-19 10:49:29 +0000183 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000184 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000185}
186
Douglas Gregord54eb442010-10-12 16:25:54 +0000187void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
188 // If this is a member template, introduce the template parameter scope.
189 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
190 if (LM.TemplateScope)
191 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
192
193 // Start the delayed C++ method declaration
194 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
195
196 // Introduce the parameters into scope and parse their default
197 // arguments.
198 ParseScope PrototypeScope(this,
199 Scope::FunctionPrototypeScope|Scope::DeclScope);
200 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
201 // Introduce the parameter into scope.
202 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
203
204 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
205 // Save the current token position.
206 SourceLocation origLoc = Tok.getLocation();
207
208 // Parse the default argument from its saved token stream.
209 Toks->push_back(Tok); // So that the current token doesn't get lost
210 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
211
212 // Consume the previously-pushed token.
213 ConsumeAnyToken();
214
215 // Consume the '='.
216 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
217 SourceLocation EqualLoc = ConsumeToken();
218
219 // The argument isn't actually potentially evaluated unless it is
220 // used.
221 EnterExpressionEvaluationContext Eval(Actions,
222 Sema::PotentiallyEvaluatedIfUsed);
223
224 ExprResult DefArgResult(ParseAssignmentExpression());
225 if (DefArgResult.isInvalid())
226 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
227 else {
228 if (Tok.is(tok::cxx_defaultarg_end))
229 ConsumeToken();
230 else
231 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
232 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
233 DefArgResult.take());
234 }
235
236 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
237 Tok.getLocation()) &&
238 "ParseAssignmentExpression went over the default arg tokens!");
239 // There could be leftover tokens (e.g. because of an error).
240 // Skip through until we reach the original token position.
241 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
242 ConsumeAnyToken();
243
244 delete Toks;
245 LM.DefaultArgs[I].Toks = 0;
246 }
247 }
248 PrototypeScope.Exit();
249
250 // Finish the delayed C++ method declaration.
251 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
252}
253
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000254/// ParseLexedMethodDefs - We finished parsing the member specification of a top
255/// (non-nested) C++ class. Now go over the stack of lexed methods that were
256/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000257void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
258 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000259 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000260 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000261 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000262
263 bool HasClassScope = !Class.TopLevelClass;
264 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
265 HasClassScope);
266
Douglas Gregord54eb442010-10-12 16:25:54 +0000267 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
268 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
269 }
270}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000271
Douglas Gregord54eb442010-10-12 16:25:54 +0000272void Parser::ParseLexedMethodDef(LexedMethod &LM) {
273 // If this is a member template, introduce the template parameter scope.
274 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
275 if (LM.TemplateScope)
276 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Douglas Gregord54eb442010-10-12 16:25:54 +0000278 // Save the current token position.
279 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000280
Douglas Gregord54eb442010-10-12 16:25:54 +0000281 assert(!LM.Toks.empty() && "Empty body!");
282 // Append the current token at the end of the new token stream so that it
283 // doesn't get lost.
284 LM.Toks.push_back(Tok);
285 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000286
Douglas Gregord54eb442010-10-12 16:25:54 +0000287 // Consume the previously pushed token.
288 ConsumeAnyToken();
289 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
290 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000291
Douglas Gregord54eb442010-10-12 16:25:54 +0000292 // Parse the method body. Function body parsing code is similar enough
293 // to be re-used for method bodies as well.
294 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
295 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000296
Douglas Gregord54eb442010-10-12 16:25:54 +0000297 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000298 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000299 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
300 Tok.getLocation()) &&
301 "ParseFunctionTryBlock went over the cached tokens!");
302 // There could be leftover tokens (e.g. because of an error).
303 // Skip through until we reach the original token position.
304 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
305 ConsumeAnyToken();
306 return;
307 }
308 if (Tok.is(tok::colon)) {
309 ParseConstructorInitializer(LM.D);
310
311 // Error recovery.
312 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000313 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000314 Actions.ActOnFinishFunctionBody(LM.D, 0);
315 return;
316 }
317 } else
318 Actions.ActOnDefaultCtorInitializers(LM.D);
319
Douglas Gregorc9977d02011-03-16 17:05:57 +0000320 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000321
322 if (Tok.getLocation() != origLoc) {
323 // Due to parsing error, we either went over the cached tokens or
324 // there are still cached tokens left. If it's the latter case skip the
325 // leftover tokens.
326 // Since this is an uncommon situation that should be avoided, use the
327 // expensive isBeforeInTranslationUnit call.
328 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
329 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000330 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000331 ConsumeAnyToken();
John McCalld6ca8da2010-04-10 07:37:23 +0000332
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000333 }
334}
335
336/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000337/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000338/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000339/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000340/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000341/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000342bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
343 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000344 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000345 // We always want this function to consume at least one token if the first
346 // token isn't T and if not at EOF.
347 bool isFirstTokenConsumed = true;
348 while (1) {
349 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000350 if (Tok.is(T1) || Tok.is(T2)) {
351 if (ConsumeFinalToken) {
352 Toks.push_back(Tok);
353 ConsumeAnyToken();
354 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000355 return true;
356 }
357
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000358 switch (Tok.getKind()) {
359 case tok::eof:
360 // Ran out of tokens.
361 return false;
362
363 case tok::l_paren:
364 // Recursively consume properly-nested parens.
365 Toks.push_back(Tok);
366 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000367 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000368 break;
369 case tok::l_square:
370 // Recursively consume properly-nested square brackets.
371 Toks.push_back(Tok);
372 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000373 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000374 break;
375 case tok::l_brace:
376 // Recursively consume properly-nested braces.
377 Toks.push_back(Tok);
378 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000379 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000380 break;
381
382 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
383 // Since the user wasn't looking for this token (if they were, it would
384 // already be handled), this isn't balanced. If there is a LHS token at a
385 // higher level, we will assume that this matches the unbalanced token
386 // and return it. Otherwise, this is a spurious RHS token, which we skip.
387 case tok::r_paren:
388 if (ParenCount && !isFirstTokenConsumed)
389 return false; // Matches something.
390 Toks.push_back(Tok);
391 ConsumeParen();
392 break;
393 case tok::r_square:
394 if (BracketCount && !isFirstTokenConsumed)
395 return false; // Matches something.
396 Toks.push_back(Tok);
397 ConsumeBracket();
398 break;
399 case tok::r_brace:
400 if (BraceCount && !isFirstTokenConsumed)
401 return false; // Matches something.
402 Toks.push_back(Tok);
403 ConsumeBrace();
404 break;
405
406 case tok::string_literal:
407 case tok::wide_string_literal:
408 Toks.push_back(Tok);
409 ConsumeStringToken();
410 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000411 case tok::semi:
412 if (StopAtSemi)
413 return false;
414 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000415 default:
416 // consume this token.
417 Toks.push_back(Tok);
418 ConsumeToken();
419 break;
420 }
421 isFirstTokenConsumed = false;
422 }
423}