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