]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/Parse/ParseCXXInlineMethods.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / lib / Parse / ParseCXXInlineMethods.cpp
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
14 #include "clang/Parse/ParseDiagnostic.h"
15 #include "clang/Parse/Parser.h"
16 #include "clang/Sema/DeclSpec.h"
17 #include "clang/Sema/Scope.h"
18 #include "clang/AST/DeclTemplate.h"
19 using namespace clang;
20
21 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
22 /// 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.
24 Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
25                                       AttributeList *AccessAttrs,
26                                       ParsingDeclarator &D,
27                                 const ParsedTemplateInfo &TemplateInfo,
28                                 const VirtSpecifiers& VS, ExprResult& Init) {
29   assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
30   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
31           Tok.is(tok::equal)) &&
32          "Current token not a '{', ':', '=', or 'try'!");
33
34   MultiTemplateParamsArg TemplateParams(Actions,
35           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
36           TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
37
38   Decl *FnD;
39   D.setFunctionDefinition(true);
40   if (D.getDeclSpec().isFriendSpecified())
41     FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
42                                           move(TemplateParams));
43   else {
44     FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
45                                            move(TemplateParams), 0, 
46                                            VS, /*HasInit=*/false);
47     if (FnD) {
48       Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
49                                        false, true);
50       bool TypeSpecContainsAuto
51         = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
52       if (Init.get())
53         Actions.AddInitializerToDecl(FnD, Init.get(), false, 
54                                      TypeSpecContainsAuto);
55       else
56         Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
57     }
58   }
59
60   HandleMemberFunctionDefaultArgs(D, FnD);
61
62   D.complete(FnD);
63
64   if (Tok.is(tok::equal)) {
65     ConsumeToken();
66
67     bool Delete = false;
68     SourceLocation KWLoc;
69     if (Tok.is(tok::kw_delete)) {
70       if (!getLang().CPlusPlus0x)
71         Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
72
73       KWLoc = ConsumeToken();
74       Actions.SetDeclDeleted(FnD, KWLoc);
75       Delete = true;
76     } else if (Tok.is(tok::kw_default)) {
77       if (!getLang().CPlusPlus0x)
78         Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
79
80       KWLoc = ConsumeToken();
81       Actions.SetDeclDefaulted(FnD, KWLoc);
82     } else {
83       llvm_unreachable("function definition after = not 'delete' or 'default'");
84     }
85
86     if (Tok.is(tok::comma)) {
87       Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
88         << Delete;
89       SkipUntil(tok::semi);
90     } else {
91       ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
92                        Delete ? "delete" : "default", tok::semi);
93     }
94
95     return FnD;
96   }
97
98   // In delayed template parsing mode, if we are within a class template
99   // or if we are about to parse function member template then consume
100   // the tokens and store them for parsing at the end of the translation unit.
101   if (getLang().DelayedTemplateParsing && 
102       ((Actions.CurContext->isDependentContext() ||
103         TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) && 
104         !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
105         !D.getDeclSpec().isFriendSpecified()) {
106
107     if (FnD) {
108       LateParsedTemplatedFunction *LPT =
109         new LateParsedTemplatedFunction(this, FnD);
110
111       FunctionDecl *FD = 0;
112       if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
113         FD = FunTmpl->getTemplatedDecl();
114       else
115         FD = cast<FunctionDecl>(FnD);
116       Actions.CheckForFunctionRedefinition(FD);
117
118       LateParsedTemplateMap[FD] = LPT;
119       Actions.MarkAsLateParsedTemplate(FD);
120       LexTemplateFunctionForLateParsing(LPT->Toks);
121     } else {
122       CachedTokens Toks;
123       LexTemplateFunctionForLateParsing(Toks);
124     }
125
126     return FnD;
127   }
128
129   // Consume the tokens and store them for later parsing.
130
131   LexedMethod* LM = new LexedMethod(this, FnD);
132   getCurrentClass().LateParsedDeclarations.push_back(LM);
133   LM->TemplateScope = getCurScope()->isTemplateParamScope();
134   CachedTokens &Toks = LM->Toks;
135
136   tok::TokenKind kind = Tok.getKind();
137   // Consume everything up to (and including) the left brace of the
138   // function body.
139   if (ConsumeAndStoreFunctionPrologue(Toks)) {
140     // We didn't find the left-brace we expected after the
141     // constructor initializer.
142     if (Tok.is(tok::semi)) {
143       // We found a semicolon; complain, consume the semicolon, and
144       // don't try to parse this method later.
145       Diag(Tok.getLocation(), diag::err_expected_lbrace);
146       ConsumeAnyToken();
147       delete getCurrentClass().LateParsedDeclarations.back();
148       getCurrentClass().LateParsedDeclarations.pop_back();
149       return FnD;
150     }
151   } else {
152     // Consume everything up to (and including) the matching right brace.
153     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
154   }
155
156   // If we're in a function-try-block, we need to store all the catch blocks.
157   if (kind == tok::kw_try) {
158     while (Tok.is(tok::kw_catch)) {
159       ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
160       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
161     }
162   }
163
164
165   if (!FnD) {
166     // If semantic analysis could not build a function declaration,
167     // just throw away the late-parsed declaration.
168     delete getCurrentClass().LateParsedDeclarations.back();
169     getCurrentClass().LateParsedDeclarations.pop_back();
170   }
171
172   return FnD;
173 }
174
175 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
176 /// specified Declarator is a well formed C++ non-static data member
177 /// declaration. Now lex its initializer and store its tokens for parsing
178 /// after the class is complete.
179 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
180   assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
181          "Current token not a '{' or '='!");
182
183   LateParsedMemberInitializer *MI =
184     new LateParsedMemberInitializer(this, VarD);
185   getCurrentClass().LateParsedDeclarations.push_back(MI);
186   CachedTokens &Toks = MI->Toks;
187
188   tok::TokenKind kind = Tok.getKind();
189   if (kind == tok::equal) {
190     Toks.push_back(Tok);
191     ConsumeAnyToken();
192   }
193
194   if (kind == tok::l_brace) {
195     // Begin by storing the '{' token.
196     Toks.push_back(Tok);
197     ConsumeBrace();
198
199     // Consume everything up to (and including) the matching right brace.
200     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
201   } else {
202     // Consume everything up to (but excluding) the comma or semicolon.
203     ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
204                          /*ConsumeFinalToken=*/false);
205   }
206
207   // Store an artificial EOF token to ensure that we don't run off the end of
208   // the initializer when we come to parse it.
209   Token Eof;
210   Eof.startToken();
211   Eof.setKind(tok::eof);
212   Eof.setLocation(Tok.getLocation());
213   Toks.push_back(Eof);
214 }
215
216 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
217 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
218 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
219 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
220
221 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
222   : Self(P), Class(C) {}
223
224 Parser::LateParsedClass::~LateParsedClass() {
225   Self->DeallocateParsedClasses(Class);
226 }
227
228 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
229   Self->ParseLexedMethodDeclarations(*Class);
230 }
231
232 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
233   Self->ParseLexedMemberInitializers(*Class);
234 }
235
236 void Parser::LateParsedClass::ParseLexedMethodDefs() {
237   Self->ParseLexedMethodDefs(*Class);
238 }
239
240 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
241   Self->ParseLexedMethodDeclaration(*this);
242 }
243
244 void Parser::LexedMethod::ParseLexedMethodDefs() {
245   Self->ParseLexedMethodDef(*this);
246 }
247
248 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
249   Self->ParseLexedMemberInitializer(*this);
250 }
251
252 /// ParseLexedMethodDeclarations - We finished parsing the member
253 /// specification of a top (non-nested) C++ class. Now go over the
254 /// stack of method declarations with some parts for which parsing was
255 /// delayed (such as default arguments) and parse them.
256 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
257   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
258   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
259   if (HasTemplateScope)
260     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
261
262   // The current scope is still active if we're the top-level class.
263   // Otherwise we'll need to push and enter a new scope.
264   bool HasClassScope = !Class.TopLevelClass;
265   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
266                         HasClassScope);
267   if (HasClassScope)
268     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
269
270   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
271     Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
272   }
273
274   if (HasClassScope)
275     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
276 }
277
278 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
279   // If this is a member template, introduce the template parameter scope.
280   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
281   if (LM.TemplateScope)
282     Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
283
284   // Start the delayed C++ method declaration
285   Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
286
287   // Introduce the parameters into scope and parse their default
288   // arguments.
289   ParseScope PrototypeScope(this,
290                             Scope::FunctionPrototypeScope|Scope::DeclScope);
291   for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
292     // Introduce the parameter into scope.
293     Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
294
295     if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
296       // Save the current token position.
297       SourceLocation origLoc = Tok.getLocation();
298
299       // Parse the default argument from its saved token stream.
300       Toks->push_back(Tok); // So that the current token doesn't get lost
301       PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
302
303       // Consume the previously-pushed token.
304       ConsumeAnyToken();
305
306       // Consume the '='.
307       assert(Tok.is(tok::equal) && "Default argument not starting with '='");
308       SourceLocation EqualLoc = ConsumeToken();
309
310       // The argument isn't actually potentially evaluated unless it is
311       // used.
312       EnterExpressionEvaluationContext Eval(Actions,
313                                             Sema::PotentiallyEvaluatedIfUsed);
314
315       ExprResult DefArgResult(ParseAssignmentExpression());
316       if (DefArgResult.isInvalid())
317         Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
318       else {
319         if (Tok.is(tok::cxx_defaultarg_end))
320           ConsumeToken();
321         else
322           Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
323         Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
324                                           DefArgResult.take());
325       }
326
327       assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
328                                                          Tok.getLocation()) &&
329              "ParseAssignmentExpression went over the default arg tokens!");
330       // There could be leftover tokens (e.g. because of an error).
331       // Skip through until we reach the original token position.
332       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
333         ConsumeAnyToken();
334
335       delete Toks;
336       LM.DefaultArgs[I].Toks = 0;
337     }
338   }
339   PrototypeScope.Exit();
340
341   // Finish the delayed C++ method declaration.
342   Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
343 }
344
345 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
346 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
347 /// collected during its parsing and parse them all.
348 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
349   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
350   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
351   if (HasTemplateScope)
352     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
353
354   bool HasClassScope = !Class.TopLevelClass;
355   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
356                         HasClassScope);
357
358   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
359     Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
360   }
361 }
362
363 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
364   // If this is a member template, introduce the template parameter scope.
365   ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
366   if (LM.TemplateScope)
367     Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
368
369   // Save the current token position.
370   SourceLocation origLoc = Tok.getLocation();
371
372   assert(!LM.Toks.empty() && "Empty body!");
373   // Append the current token at the end of the new token stream so that it
374   // doesn't get lost.
375   LM.Toks.push_back(Tok);
376   PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
377
378   // Consume the previously pushed token.
379   ConsumeAnyToken();
380   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
381          && "Inline method not starting with '{', ':' or 'try'");
382
383   // Parse the method body. Function body parsing code is similar enough
384   // to be re-used for method bodies as well.
385   ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
386   Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
387
388   if (Tok.is(tok::kw_try)) {
389     ParseFunctionTryBlock(LM.D, FnScope);
390     assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
391                                                          Tok.getLocation()) &&
392            "ParseFunctionTryBlock went over the cached tokens!");
393     // There could be leftover tokens (e.g. because of an error).
394     // Skip through until we reach the original token position.
395     while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
396       ConsumeAnyToken();
397     return;
398   }
399   if (Tok.is(tok::colon)) {
400     ParseConstructorInitializer(LM.D);
401
402     // Error recovery.
403     if (!Tok.is(tok::l_brace)) {
404       FnScope.Exit();
405       Actions.ActOnFinishFunctionBody(LM.D, 0);
406       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
407         ConsumeAnyToken();
408       return;
409     }
410   } else
411     Actions.ActOnDefaultCtorInitializers(LM.D);
412
413   ParseFunctionStatementBody(LM.D, FnScope);
414
415   if (Tok.getLocation() != origLoc) {
416     // Due to parsing error, we either went over the cached tokens or
417     // there are still cached tokens left. If it's the latter case skip the
418     // leftover tokens.
419     // Since this is an uncommon situation that should be avoided, use the
420     // expensive isBeforeInTranslationUnit call.
421     if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
422                                                         origLoc))
423       while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
424         ConsumeAnyToken();
425   }
426 }
427
428 /// ParseLexedMemberInitializers - We finished parsing the member specification
429 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
430 /// initializers that were collected during its parsing and parse them all.
431 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
432   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
433   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
434                                 HasTemplateScope);
435   if (HasTemplateScope)
436     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
437
438   // Set or update the scope flags to include Scope::ThisScope.
439   bool AlreadyHasClassScope = Class.TopLevelClass;
440   unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
441   ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
442   ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
443
444   if (!AlreadyHasClassScope)
445     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
446                                                 Class.TagOrTemplate);
447
448   for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
449     Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
450   }
451
452   if (!AlreadyHasClassScope)
453     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
454                                                  Class.TagOrTemplate);
455
456   Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
457 }
458
459 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
460   if (!MI.Field || MI.Field->isInvalidDecl())
461     return;
462
463   // Append the current token at the end of the new token stream so that it
464   // doesn't get lost.
465   MI.Toks.push_back(Tok);
466   PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
467
468   // Consume the previously pushed token.
469   ConsumeAnyToken();
470
471   SourceLocation EqualLoc;
472   ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
473
474   Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
475
476   // The next token should be our artificial terminating EOF token.
477   if (Tok.isNot(tok::eof)) {
478     SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
479     if (!EndLoc.isValid())
480       EndLoc = Tok.getLocation();
481     // No fixit; we can't recover as if there were a semicolon here.
482     Diag(EndLoc, diag::err_expected_semi_decl_list);
483
484     // Consume tokens until we hit the artificial EOF.
485     while (Tok.isNot(tok::eof))
486       ConsumeAnyToken();
487   }
488   ConsumeAnyToken();
489 }
490
491 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
492 /// container until the token 'T' is reached (which gets
493 /// consumed/stored too, if ConsumeFinalToken).
494 /// If StopAtSemi is true, then we will stop early at a ';' character.
495 /// Returns true if token 'T1' or 'T2' was found.
496 /// NOTE: This is a specialized version of Parser::SkipUntil.
497 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
498                                   CachedTokens &Toks,
499                                   bool StopAtSemi, bool ConsumeFinalToken) {
500   // We always want this function to consume at least one token if the first
501   // token isn't T and if not at EOF.
502   bool isFirstTokenConsumed = true;
503   while (1) {
504     // If we found one of the tokens, stop and return true.
505     if (Tok.is(T1) || Tok.is(T2)) {
506       if (ConsumeFinalToken) {
507         Toks.push_back(Tok);
508         ConsumeAnyToken();
509       }
510       return true;
511     }
512
513     switch (Tok.getKind()) {
514     case tok::eof:
515       // Ran out of tokens.
516       return false;
517
518     case tok::l_paren:
519       // Recursively consume properly-nested parens.
520       Toks.push_back(Tok);
521       ConsumeParen();
522       ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
523       break;
524     case tok::l_square:
525       // Recursively consume properly-nested square brackets.
526       Toks.push_back(Tok);
527       ConsumeBracket();
528       ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
529       break;
530     case tok::l_brace:
531       // Recursively consume properly-nested braces.
532       Toks.push_back(Tok);
533       ConsumeBrace();
534       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
535       break;
536
537     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
538     // Since the user wasn't looking for this token (if they were, it would
539     // already be handled), this isn't balanced.  If there is a LHS token at a
540     // higher level, we will assume that this matches the unbalanced token
541     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
542     case tok::r_paren:
543       if (ParenCount && !isFirstTokenConsumed)
544         return false;  // Matches something.
545       Toks.push_back(Tok);
546       ConsumeParen();
547       break;
548     case tok::r_square:
549       if (BracketCount && !isFirstTokenConsumed)
550         return false;  // Matches something.
551       Toks.push_back(Tok);
552       ConsumeBracket();
553       break;
554     case tok::r_brace:
555       if (BraceCount && !isFirstTokenConsumed)
556         return false;  // Matches something.
557       Toks.push_back(Tok);
558       ConsumeBrace();
559       break;
560
561     case tok::code_completion:
562       Toks.push_back(Tok);
563       ConsumeCodeCompletionToken();
564       break;
565
566     case tok::string_literal:
567     case tok::wide_string_literal:
568     case tok::utf8_string_literal:
569     case tok::utf16_string_literal:
570     case tok::utf32_string_literal:
571       Toks.push_back(Tok);
572       ConsumeStringToken();
573       break;
574     case tok::semi:
575       if (StopAtSemi)
576         return false;
577       // FALL THROUGH.
578     default:
579       // consume this token.
580       Toks.push_back(Tok);
581       ConsumeToken();
582       break;
583     }
584     isFirstTokenConsumed = false;
585   }
586 }
587
588 /// \brief Consume tokens and store them in the passed token container until
589 /// we've passed the try keyword and constructor initializers and have consumed
590 /// the opening brace of the function body. The opening brace will be consumed
591 /// if and only if there was no error.
592 ///
593 /// \return True on error. 
594 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
595   if (Tok.is(tok::kw_try)) {
596     Toks.push_back(Tok);
597     ConsumeToken();
598   }
599   if (Tok.is(tok::colon)) {
600     // Initializers can contain braces too.
601     Toks.push_back(Tok);
602     ConsumeToken();
603
604     while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
605       if (Tok.is(tok::eof) || Tok.is(tok::semi))
606         return true;
607
608       // Grab the identifier.
609       if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
610                                 /*StopAtSemi=*/true,
611                                 /*ConsumeFinalToken=*/false))
612         return true;
613
614       tok::TokenKind kind = Tok.getKind();
615       Toks.push_back(Tok);
616       if (kind == tok::l_paren)
617         ConsumeParen();
618       else {
619         assert(kind == tok::l_brace && "Must be left paren or brace here.");
620         ConsumeBrace();
621         // In C++03, this has to be the start of the function body, which
622         // means the initializer is malformed.
623         if (!getLang().CPlusPlus0x)
624           return false;
625       }
626
627       // Grab the initializer
628       if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
629                                                        tok::r_brace,
630                                 Toks, /*StopAtSemi=*/true))
631         return true;
632
633       // Grab the separating comma, if any.
634       if (Tok.is(tok::comma)) {
635         Toks.push_back(Tok);
636         ConsumeToken();
637       }
638     }
639   }
640
641   // Grab any remaining garbage to be diagnosed later. We stop when we reach a
642   // brace: an opening one is the function body, while a closing one probably
643   // means we've reached the end of the class.
644   if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
645                             /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
646     return true;
647   if(Tok.isNot(tok::l_brace))
648     return true;
649
650   Toks.push_back(Tok);
651   ConsumeBrace();
652   return false;
653 }