14cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
24cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//
34cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
44cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//
54cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis// This file is distributed under the University of Illinois Open Source
64cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis// License. See LICENSE.TXT for details.
74cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//
84cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
94cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//
104cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//  This file implements parsing for C++ class inline methods.
114cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//
124cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
134cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
144cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis#include "clang/Parse/Parser.h"
1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "RAIIObjectsForParser.h"
1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclTemplate.h"
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Parse/ParseDiagnostic.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
204cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidisusing namespace clang;
214cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
22d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl/// ParseCXXInlineMethodDef - We parsed and verified that the specified
234cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// Declarator is a well formed C++ inline method definition. Now lex its body
244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// and store its tokens for parsing after the C++ class is complete.
250777cf5fcb8ca53bdc7e6455e9068742fbcc14dbRafael EspindolaNamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
265f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen                                      AttributeList *AccessAttrs,
275f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen                                      ParsingDeclarator &D,
2845fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor                                      const ParsedTemplateInfo &TemplateInfo,
293ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar                                      const VirtSpecifiers& VS,
3045fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor                                      ExprResult& Init) {
31075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
32e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
33e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt          Tok.is(tok::equal)) &&
34e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt         "Current token not a '{', ':', '=', or 'try'!");
354cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
365354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  MultiTemplateParamsArg TemplateParams(
376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                  : nullptr,
396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
404cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt
410777cf5fcb8ca53bdc7e6455e9068742fbcc14dbRafael Espindola  NamedDecl *FnD;
4267d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall  if (D.getDeclSpec().isFriendSpecified())
432c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
443fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                          TemplateParams);
45147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor  else {
4623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                           TemplateParams, nullptr,
48ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                           VS, ICIS_NoInit);
49147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    if (FnD) {
504a97b8e75f5dccf5a9537cf7358297437f55326dRichard Smith      Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
51a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
52a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor      if (Init.isUsable())
53ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Actions.AddInitializerToDecl(FnD, Init.get(), false,
54147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor                                     TypeSpecContainsAuto);
55147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      else
56147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor        Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
57147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    }
584867347e82648d3baf09524b98b09c297a5a198fNico Weber  }
594cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
6074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor  HandleMemberFunctionDeclDelays(D, FnD);
61d33133cdc1af466f9c276249b2621be03867888bEli Friedman
62eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  D.complete(FnD);
63eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
64651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TryConsumeToken(tok::equal)) {
65c430ef4f92eedea7d3378f05a2b05982ed3713dcRichard Smith    if (!FnD) {
66c430ef4f92eedea7d3378f05a2b05982ed3713dcRichard Smith      SkipUntil(tok::semi);
676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
68c430ef4f92eedea7d3378f05a2b05982ed3713dcRichard Smith    }
69c430ef4f92eedea7d3378f05a2b05982ed3713dcRichard Smith
70e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    bool Delete = false;
71e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    SourceLocation KWLoc;
723ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
73651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TryConsumeToken(tok::kw_delete, KWLoc)) {
74651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(KWLoc, getLangOpts().CPlusPlus11
75651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      ? diag::warn_cxx98_compat_deleted_function
76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      : diag::ext_deleted_function);
77e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Actions.SetDeclDeleted(FnD, KWLoc);
78e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Delete = true;
793ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
803ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        DeclAsFunction->setRangeEnd(KWEndLoc);
813ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      }
82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(KWLoc, getLangOpts().CPlusPlus11
84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      ? diag::warn_cxx98_compat_defaulted_function
85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      : diag::ext_defaulted_function);
86e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Actions.SetDeclDefaulted(FnD, KWLoc);
873ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
883ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        DeclAsFunction->setRangeEnd(KWEndLoc);
893ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      }
90e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else {
91e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
92e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
93e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
94e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (Tok.is(tok::comma)) {
95e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
96e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        << Delete;
97e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      SkipUntil(tok::semi);
98651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
99651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                Delete ? "delete" : "default")) {
100651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SkipUntil(tok::semi);
101e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
102e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
103e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return FnD;
104e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
1053ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar
1068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, if we are within a class template
1078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // or if we are about to parse function member template then consume
1088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // the tokens and store them for parsing at the end of the translation unit.
109abcfa61f513e499fe3f2f44df9cb48e133fc4fc0David Majnemer  if (getLangOpts().DelayedTemplateParsing &&
1103ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      D.getFunctionDefinitionKind() == FDK_Definition &&
111651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      !D.getDeclSpec().isConstexprSpecified() &&
112651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      !(FnD && FnD->getAsFunction() &&
113651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
1148387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      ((Actions.CurContext->isDependentContext() ||
115abcfa61f513e499fe3f2f44df9cb48e133fc4fc0David Majnemer        (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
116abcfa61f513e499fe3f2f44df9cb48e133fc4fc0David Majnemer         TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
117abcfa61f513e499fe3f2f44df9cb48e133fc4fc0David Majnemer       !Actions.IsInsideALocalClassWithinATemplateFunction())) {
1188387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
119ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    CachedTokens Toks;
120ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LexTemplateFunctionForLateParsing(Toks);
1218387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
122ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    if (FnD) {
123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      FunctionDecl *FD = FnD->getAsFunction();
12481542fd91bd5e7e65ebae3eaad117bdaeaf7d737Chandler Carruth      Actions.CheckForFunctionRedefinition(FD);
125ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith      Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
1268387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
1278387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
1288387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return FnD;
1298387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
1308387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
1314cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // Consume the tokens and store them for later parsing.
1324cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
133d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  LexedMethod* LM = new LexedMethod(this, FnD);
134d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  getCurrentClass().LateParsedDeclarations.push_back(LM);
135d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  LM->TemplateScope = getCurScope()->isTemplateParamScope();
136d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  CachedTokens &Toks = LM->Toks;
1374cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
138d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  tok::TokenKind kind = Tok.getKind();
139a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl  // Consume everything up to (and including) the left brace of the
140a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl  // function body.
141a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl  if (ConsumeAndStoreFunctionPrologue(Toks)) {
142a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl    // We didn't find the left-brace we expected after the
143e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman    // constructor initializer; we already printed an error, and it's likely
144e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman    // impossible to recover, so don't try to parse this method later.
14554ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // Skip over the rest of the decl and back to somewhere that looks
14654ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // reasonable.
14754ca069d1526df84a48a3974871a7c4f46549651Richard Smith    SkipMalformedDecl();
148e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman    delete getCurrentClass().LateParsedDeclarations.back();
149e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman    getCurrentClass().LateParsedDeclarations.pop_back();
150e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman    return FnD;
1517ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  } else {
152a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl    // Consume everything up to (and including) the matching right brace.
153a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1547ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  }
1554cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
156d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  // If we're in a function-try-block, we need to store all the catch blocks.
157d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (kind == tok::kw_try) {
158d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    while (Tok.is(tok::kw_catch)) {
15914b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
16014b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
161d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    }
162d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  }
163d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
164319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin  if (FnD) {
165319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // If this is a friend function, mark that it's late-parsed so that
166319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // it's still known to be a definition even before we attach the
167319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // parsed body.  Sema needs to treat friend function definitions
168319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // differently during template instantiation, and it's possible for
169319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // the containing class to be instantiated before all its member
170319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // function definitions are parsed.
171319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    //
172319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // If you remove this, you can remove the code that clears the flag
173319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    // after parsing the member.
174319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    if (D.getDeclSpec().isFriendSpecified()) {
175651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      FunctionDecl *FD = FnD->getAsFunction();
176530fa4fc800f2788cfe6d4113677ca631d1e801fAlp Toker      Actions.CheckForFunctionRedefinition(FD);
177530fa4fc800f2788cfe6d4113677ca631d1e801fAlp Toker      FD->setLateTemplateParsed(true);
178319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin    }
179319957c64a445b6e16bf00054fc4bb88754abfc8Stephen Lin  } else {
18087f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    // If semantic analysis could not build a function declaration,
18187f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    // just throw away the late-parsed declaration.
18287f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    delete getCurrentClass().LateParsedDeclarations.back();
18387f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    getCurrentClass().LateParsedDeclarations.pop_back();
18487f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor  }
18587f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor
1864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  return FnD;
1874cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
1884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
1897a614d8380297fcd2bc23986241905d97222948cRichard Smith/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
1907a614d8380297fcd2bc23986241905d97222948cRichard Smith/// specified Declarator is a well formed C++ non-static data member
1917a614d8380297fcd2bc23986241905d97222948cRichard Smith/// declaration. Now lex its initializer and store its tokens for parsing
1927a614d8380297fcd2bc23986241905d97222948cRichard Smith/// after the class is complete.
1937a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
1947a614d8380297fcd2bc23986241905d97222948cRichard Smith  assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
1957a614d8380297fcd2bc23986241905d97222948cRichard Smith         "Current token not a '{' or '='!");
1967a614d8380297fcd2bc23986241905d97222948cRichard Smith
1977a614d8380297fcd2bc23986241905d97222948cRichard Smith  LateParsedMemberInitializer *MI =
1987a614d8380297fcd2bc23986241905d97222948cRichard Smith    new LateParsedMemberInitializer(this, VarD);
1997a614d8380297fcd2bc23986241905d97222948cRichard Smith  getCurrentClass().LateParsedDeclarations.push_back(MI);
2007a614d8380297fcd2bc23986241905d97222948cRichard Smith  CachedTokens &Toks = MI->Toks;
2017a614d8380297fcd2bc23986241905d97222948cRichard Smith
2027a614d8380297fcd2bc23986241905d97222948cRichard Smith  tok::TokenKind kind = Tok.getKind();
2037a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (kind == tok::equal) {
2047a614d8380297fcd2bc23986241905d97222948cRichard Smith    Toks.push_back(Tok);
205d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    ConsumeToken();
2067a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
2077a614d8380297fcd2bc23986241905d97222948cRichard Smith
2087a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (kind == tok::l_brace) {
2097a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Begin by storing the '{' token.
2107a614d8380297fcd2bc23986241905d97222948cRichard Smith    Toks.push_back(Tok);
2117a614d8380297fcd2bc23986241905d97222948cRichard Smith    ConsumeBrace();
2127a614d8380297fcd2bc23986241905d97222948cRichard Smith
2137a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Consume everything up to (and including) the matching right brace.
2147a614d8380297fcd2bc23986241905d97222948cRichard Smith    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
2157a614d8380297fcd2bc23986241905d97222948cRichard Smith  } else {
2167a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Consume everything up to (but excluding) the comma or semicolon.
2179bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
2187a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
2197a614d8380297fcd2bc23986241905d97222948cRichard Smith
2207a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Store an artificial EOF token to ensure that we don't run off the end of
2217a614d8380297fcd2bc23986241905d97222948cRichard Smith  // the initializer when we come to parse it.
2227a614d8380297fcd2bc23986241905d97222948cRichard Smith  Token Eof;
2237a614d8380297fcd2bc23986241905d97222948cRichard Smith  Eof.startToken();
2247a614d8380297fcd2bc23986241905d97222948cRichard Smith  Eof.setKind(tok::eof);
2257a614d8380297fcd2bc23986241905d97222948cRichard Smith  Eof.setLocation(Tok.getLocation());
2260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  Eof.setEofData(VarD);
2277a614d8380297fcd2bc23986241905d97222948cRichard Smith  Toks.push_back(Eof);
2287a614d8380297fcd2bc23986241905d97222948cRichard Smith}
2297a614d8380297fcd2bc23986241905d97222948cRichard Smith
230d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas GregorParser::LateParsedDeclaration::~LateParsedDeclaration() {}
231d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
2327a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
233d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
234d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
235d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas GregorParser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
236d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  : Self(P), Class(C) {}
237d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
238d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas GregorParser::LateParsedClass::~LateParsedClass() {
239d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->DeallocateParsedClasses(Class);
240d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
241d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
242d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedClass::ParseLexedMethodDeclarations() {
243d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDeclarations(*Class);
244d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
245d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
2467a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::LateParsedClass::ParseLexedMemberInitializers() {
2477a614d8380297fcd2bc23986241905d97222948cRichard Smith  Self->ParseLexedMemberInitializers(*Class);
2487a614d8380297fcd2bc23986241905d97222948cRichard Smith}
2497a614d8380297fcd2bc23986241905d97222948cRichard Smith
250d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedClass::ParseLexedMethodDefs() {
251d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDefs(*Class);
252d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
253d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
254d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
255d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDeclaration(*this);
256d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
257d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
258d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LexedMethod::ParseLexedMethodDefs() {
259d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDef(*this);
260d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
261d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
2627a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
2637a614d8380297fcd2bc23986241905d97222948cRichard Smith  Self->ParseLexedMemberInitializer(*this);
2647a614d8380297fcd2bc23986241905d97222948cRichard Smith}
2657a614d8380297fcd2bc23986241905d97222948cRichard Smith
26672b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// ParseLexedMethodDeclarations - We finished parsing the member
26772b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// specification of a top (non-nested) C++ class. Now go over the
26872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// stack of method declarations with some parts for which parsing was
26972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// delayed (such as default arguments) and parse them.
2706569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
2716569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
2726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
2736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                HasTemplateScope);
274098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
275098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  if (HasTemplateScope) {
27623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
277098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith    ++CurTemplateDepthTracker;
278098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  }
2796569d68745c8213709740337d2be52b031384f58Douglas Gregor
2807a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // The current scope is still active if we're the top-level class.
2817a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // Otherwise we'll need to push and enter a new scope.
2826569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasClassScope = !Class.TopLevelClass;
2834cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt  ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
2844cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt                        HasClassScope);
2857a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (HasClassScope)
2866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
2876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                Class.TagOrTemplate);
2886569d68745c8213709740337d2be52b031384f58Douglas Gregor
289d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
290d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
291d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  }
29272b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
293d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (HasClassScope)
2946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
2956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                 Class.TagOrTemplate);
296d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
2977fd3a6456a88cdd225b92ae1606144f24c7f51d4Argyrios Kyrtzidis
298d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
299d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // If this is a member template, introduce the template parameter scope.
300d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
301098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
302098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  if (LM.TemplateScope) {
303d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
304098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith    ++CurTemplateDepthTracker;
305098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  }
306d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Start the delayed C++ method declaration
307d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
308d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
309d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Introduce the parameters into scope and parse their default
310d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // arguments.
3113a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
3123a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                            Scope::FunctionDeclarationScope | Scope::DeclScope);
313d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
3140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
315d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Introduce the parameter into scope.
3160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    bool HasUnparsed = Param->hasUnparsedDefaultArg();
3170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
318d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
3190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      // Mark the end of the default argument so that we know when to stop when
3200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      // we parse it later on.
3210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      Token LastDefaultArgToken = Toks->back();
3220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      Token DefArgEnd;
3230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      DefArgEnd.startToken();
3240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      DefArgEnd.setKind(tok::eof);
3253ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
3260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      DefArgEnd.setEofData(Param);
3270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      Toks->push_back(DefArgEnd);
328d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
329d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Parse the default argument from its saved token stream.
330d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      Toks->push_back(Tok); // So that the current token doesn't get lost
331d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
332d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
333d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Consume the previously-pushed token.
334d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      ConsumeAnyToken();
335d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
336d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Consume the '='.
337d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      assert(Tok.is(tok::equal) && "Default argument not starting with '='");
338d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      SourceLocation EqualLoc = ConsumeToken();
339d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
340d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // The argument isn't actually potentially evaluated unless it is
341d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // used.
342d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      EnterExpressionEvaluationContext Eval(Actions,
343ccc1b5eebc6ca8a904c58c0468b9a71483b7c7cfDouglas Gregor                                            Sema::PotentiallyEvaluatedIfUsed,
3440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                            Param);
345d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
34684407ba82a10235962901ce269b7b3276d17f01dSebastian Redl      ExprResult DefArgResult;
34780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
348ca8937111cccdbf7d17c349487a332d6c7c97f91Sebastian Redl        Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
34984407ba82a10235962901ce269b7b3276d17f01dSebastian Redl        DefArgResult = ParseBraceInitializer();
350ca8937111cccdbf7d17c349487a332d6c7c97f91Sebastian Redl      } else
35184407ba82a10235962901ce269b7b3276d17f01dSebastian Redl        DefArgResult = ParseAssignmentExpression();
352176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
3530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (DefArgResult.isInvalid()) {
3540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
3550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      } else {
3560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
3579bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // The last two tokens are the terminator and the saved value of
3589bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // Tok; the last token in the default argument is the one before
3599bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // those.
3609bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          assert(Toks->size() >= 3 && "expected a token in default arg");
3619bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
3629bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith            << SourceRange(Tok.getLocation(),
3639bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith                           (*Toks)[Toks->size() - 3].getLocation());
3649bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        }
3650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Actions.ActOnParamDefaultArgument(Param, EqualLoc,
366c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                          DefArgResult.get());
367d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      }
36872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
369d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // There could be leftover tokens (e.g. because of an error).
3700e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      // Skip through until we reach the 'end of default argument' token.
3710e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      while (Tok.isNot(tok::eof))
3720e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ConsumeAnyToken();
3730e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3740e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Tok.is(tok::eof) && Tok.getEofData() == Param)
37572b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor        ConsumeAnyToken();
37672b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
377d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      delete Toks;
3786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      LM.DefaultArgs[I].Toks = nullptr;
3790e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    } else if (HasUnparsed) {
3800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      assert(Param->hasInheritedDefaultArg());
3810e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
3820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      ParmVarDecl *OldParam = Old->getParamDecl(I);
3830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      assert (!OldParam->hasUnparsedDefaultArg());
3840e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (OldParam->hasUninstantiatedDefaultArg())
3850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Param->setUninstantiatedDefaultArg(
3860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                      Param->getUninstantiatedDefaultArg());
3870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      else
3880e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Param->setDefaultArg(OldParam->getInit());
38972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor    }
39072b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor  }
391a058fd4f0a944174295f77169b438510dad389f8Richard Smith
392176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // Parse a delayed exception-specification, if there is one.
393176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
3940e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // Add the 'stop' token.
3950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Token LastExceptionSpecToken = Toks->back();
3960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Token ExceptionSpecEnd;
3970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ExceptionSpecEnd.startToken();
3980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ExceptionSpecEnd.setKind(tok::eof);
3993ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
4000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ExceptionSpecEnd.setEofData(LM.Method);
4010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Toks->push_back(ExceptionSpecEnd);
402176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
403176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Parse the default argument from its saved token stream.
404176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Toks->push_back(Tok); // So that the current token doesn't get lost
405176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
406176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
407176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Consume the previously-pushed token.
408176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ConsumeAnyToken();
409176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
410176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // C++11 [expr.prim.general]p3:
411176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    //   If a declaration declares a member function or member function
412176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    //   template of a class X, the expression this is a prvalue of type
413176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
414176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    //   and the end of the function-definition, member-declarator, or
415176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    //   declarator.
416176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    CXXMethodDecl *Method;
417176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (FunctionTemplateDecl *FunTmpl
418176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          = dyn_cast<FunctionTemplateDecl>(LM.Method))
419176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
420176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    else
421176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Method = cast<CXXMethodDecl>(LM.Method);
422176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
423176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
424176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                     Method->getTypeQualifiers(),
425176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                     getLangOpts().CPlusPlus11);
426176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
427176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Parse the exception-specification.
428176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SourceRange SpecificationRange;
429176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SmallVector<ParsedType, 4> DynamicExceptions;
430176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SmallVector<SourceRange, 4> DynamicExceptionRanges;
431176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExprResult NoexceptExpr;
432176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    CachedTokens *ExceptionSpecTokens;
433176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
434176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExceptionSpecificationType EST
435176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
436176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                       DynamicExceptions,
437176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                       DynamicExceptionRanges, NoexceptExpr,
438176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                       ExceptionSpecTokens);
439176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
4400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
441176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
442176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
443176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Attach the exception-specification to the method.
444176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
445176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                               SpecificationRange,
446176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                               DynamicExceptions,
447176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                               DynamicExceptionRanges,
448176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                               NoexceptExpr.isUsable()?
449176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                                 NoexceptExpr.get() : nullptr);
450176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
451176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // There could be leftover tokens (e.g. because of an error).
452176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Skip through until we reach the original token position.
4530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    while (Tok.isNot(tok::eof))
4540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      ConsumeAnyToken();
4550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
4560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // Clean up the remaining EOF token.
4570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
458176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ConsumeAnyToken();
459176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
460176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    delete Toks;
461176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    LM.ExceptionSpecTokens = nullptr;
462176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
463176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
464d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  PrototypeScope.Exit();
4656569d68745c8213709740337d2be52b031384f58Douglas Gregor
466d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Finish the delayed C++ method declaration.
467d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
46872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor}
46972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
4704cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseLexedMethodDefs - We finished parsing the member specification of a top
4714cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// (non-nested) C++ class. Now go over the stack of lexed methods that were
4724cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// collected during its parsing and parse them all.
4736569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::ParseLexedMethodDefs(ParsingClass &Class) {
4746569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
475d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
476098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
477098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  if (HasTemplateScope) {
47823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
479098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith    ++CurTemplateDepthTracker;
480098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  }
4816569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasClassScope = !Class.TopLevelClass;
4826569d68745c8213709740337d2be52b031384f58Douglas Gregor  ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
4836569d68745c8213709740337d2be52b031384f58Douglas Gregor                        HasClassScope);
4846569d68745c8213709740337d2be52b031384f58Douglas Gregor
485d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
486d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
487d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  }
488d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
4894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
490d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::ParseLexedMethodDef(LexedMethod &LM) {
491d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // If this is a member template, introduce the template parameter scope.
492d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
493098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
494098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  if (LM.TemplateScope) {
495d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
496098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith    ++CurTemplateDepthTracker;
497098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  }
498d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
499d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  assert(!LM.Toks.empty() && "Empty body!");
5000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  Token LastBodyToken = LM.Toks.back();
5010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  Token BodyEnd;
5020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  BodyEnd.startToken();
5030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  BodyEnd.setKind(tok::eof);
5043ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  BodyEnd.setLocation(LastBodyToken.getEndLoc());
5050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  BodyEnd.setEofData(LM.D);
5060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  LM.Toks.push_back(BodyEnd);
507d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Append the current token at the end of the new token stream so that it
508d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // doesn't get lost.
509d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  LM.Toks.push_back(Tok);
510d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
511d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
512d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Consume the previously pushed token.
513ab2d09b6287a5dc6fa94d78739444f46f9a78bfbArgyrios Kyrtzidis  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
514d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
515d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor         && "Inline method not starting with '{', ':' or 'try'");
516d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
517d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Parse the method body. Function body parsing code is similar enough
518d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // to be re-used for method bodies as well.
519d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
520d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
521d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
522d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (Tok.is(tok::kw_try)) {
523c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    ParseFunctionTryBlock(LM.D, FnScope);
5240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
5250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    while (Tok.isNot(tok::eof))
5260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      ConsumeAnyToken();
5270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
5280e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
529d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      ConsumeAnyToken();
530d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    return;
531d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  }
532d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (Tok.is(tok::colon)) {
533d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    ParseConstructorInitializer(LM.D);
5344cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
535d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Error recovery.
536d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    if (!Tok.is(tok::l_brace)) {
537c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      FnScope.Exit();
5386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Actions.ActOnFinishFunctionBody(LM.D, nullptr);
5390e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
5400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      while (Tok.isNot(tok::eof))
5410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ConsumeAnyToken();
5420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
5430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
54410904527359605a76561a49769a2199351a072d8Matt Beaumont-Gay        ConsumeAnyToken();
545d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      return;
546d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    }
547d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  } else
548d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnDefaultCtorInitializers(LM.D);
549d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
550098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  assert((Actions.getDiagnostics().hasErrorOccurred() ||
551098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith          !isa<FunctionTemplateDecl>(LM.D) ||
552098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith          cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
553098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith            < TemplateParameterDepth) &&
554098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith         "TemplateParameterDepth should be greater than the depth of "
555098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith         "current template being instantiated!");
556098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith
557c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  ParseFunctionStatementBody(LM.D, FnScope);
558d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
559e34db6b3e772e9832e1f6de1f23457076ffbec88John McCall  // Clear the late-template-parsed bit if we set it before.
560651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (LM.D)
561651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    LM.D->getAsFunction()->setLateTemplateParsed(false);
562e34db6b3e772e9832e1f6de1f23457076ffbec88John McCall
5630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while (Tok.isNot(tok::eof))
5640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ConsumeAnyToken();
5650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
5660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
5670e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ConsumeAnyToken();
5686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(LM.D))
5706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Actions.ActOnFinishInlineMethodDef(MD);
5717a614d8380297fcd2bc23986241905d97222948cRichard Smith}
5727a614d8380297fcd2bc23986241905d97222948cRichard Smith
5737a614d8380297fcd2bc23986241905d97222948cRichard Smith/// ParseLexedMemberInitializers - We finished parsing the member specification
5747a614d8380297fcd2bc23986241905d97222948cRichard Smith/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
5757a614d8380297fcd2bc23986241905d97222948cRichard Smith/// initializers that were collected during its parsing and parse them all.
5767a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
5777a614d8380297fcd2bc23986241905d97222948cRichard Smith  bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
5787a614d8380297fcd2bc23986241905d97222948cRichard Smith  ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
5797a614d8380297fcd2bc23986241905d97222948cRichard Smith                                HasTemplateScope);
580098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
581098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  if (HasTemplateScope) {
5827a614d8380297fcd2bc23986241905d97222948cRichard Smith    Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
583098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith    ++CurTemplateDepthTracker;
584098b8140c082e5a69b18ca83ef2eb3091939d54fRichard Smith  }
585cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // Set or update the scope flags.
5867a614d8380297fcd2bc23986241905d97222948cRichard Smith  bool AlreadyHasClassScope = Class.TopLevelClass;
587cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
5887a614d8380297fcd2bc23986241905d97222948cRichard Smith  ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
5897a614d8380297fcd2bc23986241905d97222948cRichard Smith  ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
5907558cd00a5ac620990174f3cba86aea4bd9a000aArgyrios Kyrtzidis
5917a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (!AlreadyHasClassScope)
5927a614d8380297fcd2bc23986241905d97222948cRichard Smith    Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
5937a614d8380297fcd2bc23986241905d97222948cRichard Smith                                                Class.TagOrTemplate);
5947a614d8380297fcd2bc23986241905d97222948cRichard Smith
595268efba18e171d47e847ccdf313498905c32acfaBenjamin Kramer  if (!Class.LateParsedDeclarations.empty()) {
596cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    // C++11 [expr.prim.general]p4:
597cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    //   Otherwise, if a member-declarator declares a non-static data member
598cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    //  (9.2) of a class X, the expression this is a prvalue of type "pointer
599cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    //  to X" within the optional brace-or-equal-initializer. It shall not
600cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    //  appear elsewhere in the member-declarator.
601cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
602cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                     /*TypeQuals=*/(unsigned)0);
603cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
604cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
605cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor      Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
606cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    }
6074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
608cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
6097a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (!AlreadyHasClassScope)
6107a614d8380297fcd2bc23986241905d97222948cRichard Smith    Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
6117a614d8380297fcd2bc23986241905d97222948cRichard Smith                                                 Class.TagOrTemplate);
6127a614d8380297fcd2bc23986241905d97222948cRichard Smith
6137a614d8380297fcd2bc23986241905d97222948cRichard Smith  Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
6147a614d8380297fcd2bc23986241905d97222948cRichard Smith}
6157a614d8380297fcd2bc23986241905d97222948cRichard Smith
6167a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
6171991b7139a92d73420336690a870f049e88cfad6Richard Smith  if (!MI.Field || MI.Field->isInvalidDecl())
6187a614d8380297fcd2bc23986241905d97222948cRichard Smith    return;
6197a614d8380297fcd2bc23986241905d97222948cRichard Smith
6207a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Append the current token at the end of the new token stream so that it
6217a614d8380297fcd2bc23986241905d97222948cRichard Smith  // doesn't get lost.
6227a614d8380297fcd2bc23986241905d97222948cRichard Smith  MI.Toks.push_back(Tok);
6237a614d8380297fcd2bc23986241905d97222948cRichard Smith  PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
6247a614d8380297fcd2bc23986241905d97222948cRichard Smith
6257a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Consume the previously pushed token.
626ab2d09b6287a5dc6fa94d78739444f46f9a78bfbArgyrios Kyrtzidis  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
6277a614d8380297fcd2bc23986241905d97222948cRichard Smith
6287a614d8380297fcd2bc23986241905d97222948cRichard Smith  SourceLocation EqualLoc;
629ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith
630651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Actions.ActOnStartCXXInClassMemberInitializer();
631651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
632552e29985a710f4ced62b39d70557501bd31ca9bDouglas Gregor  ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
633552e29985a710f4ced62b39d70557501bd31ca9bDouglas Gregor                                              EqualLoc);
6347a614d8380297fcd2bc23986241905d97222948cRichard Smith
635651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
636c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                                 Init.get());
6377a614d8380297fcd2bc23986241905d97222948cRichard Smith
6387a614d8380297fcd2bc23986241905d97222948cRichard Smith  // The next token should be our artificial terminating EOF token.
6397a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (Tok.isNot(tok::eof)) {
640176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (!Init.isInvalid()) {
641176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
642176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (!EndLoc.isValid())
643176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        EndLoc = Tok.getLocation();
644176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // No fixit; we can't recover as if there were a semicolon here.
645176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(EndLoc, diag::err_expected_semi_decl_list);
646176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
6477a614d8380297fcd2bc23986241905d97222948cRichard Smith
6487a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Consume tokens until we hit the artificial EOF.
6497a614d8380297fcd2bc23986241905d97222948cRichard Smith    while (Tok.isNot(tok::eof))
6507a614d8380297fcd2bc23986241905d97222948cRichard Smith      ConsumeAnyToken();
6517a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
6520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Make sure this is *our* artificial EOF token.
6530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (Tok.getEofData() == MI.Field)
6540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ConsumeAnyToken();
6554cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
6564cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
6574cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ConsumeAndStoreUntil - Consume and store the token at the passed token
65872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// container until the token 'T' is reached (which gets
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// consumed/stored too, if ConsumeFinalToken).
66014b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis/// If StopAtSemi is true, then we will stop early at a ';' character.
66172b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// Returns true if token 'T1' or 'T2' was found.
6624cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// NOTE: This is a specialized version of Parser::SkipUntil.
66372b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregorbool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
66472b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor                                  CachedTokens &Toks,
66514b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis                                  bool StopAtSemi, bool ConsumeFinalToken) {
6664cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // We always want this function to consume at least one token if the first
6674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // token isn't T and if not at EOF.
6684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  bool isFirstTokenConsumed = true;
6694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  while (1) {
6704cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // If we found one of the tokens, stop and return true.
67172b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor    if (Tok.is(T1) || Tok.is(T2)) {
67272b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor      if (ConsumeFinalToken) {
67372b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor        Toks.push_back(Tok);
67472b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor        ConsumeAnyToken();
67572b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor      }
6764cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      return true;
6774cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    }
6784cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
6794cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    switch (Tok.getKind()) {
6804cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::eof:
681651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_begin:
682651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_end:
683651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_include:
6844cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Ran out of tokens.
6854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      return false;
6864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
6874cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::l_paren:
6884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Recursively consume properly-nested parens.
6894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
6904cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeParen();
69114b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
6924cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
6934cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::l_square:
6944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Recursively consume properly-nested square brackets.
6954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
6964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBracket();
69714b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
6984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
6994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::l_brace:
7004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Recursively consume properly-nested braces.
7014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
7024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBrace();
70314b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
7044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
7054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
7064cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
7074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // Since the user wasn't looking for this token (if they were, it would
7084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // already be handled), this isn't balanced.  If there is a LHS token at a
7094cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // higher level, we will assume that this matches the unbalanced token
7104cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
7114cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::r_paren:
7124cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      if (ParenCount && !isFirstTokenConsumed)
7134cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis        return false;  // Matches something.
7144cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
7154cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeParen();
7164cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
7174cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::r_square:
7184cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      if (BracketCount && !isFirstTokenConsumed)
7194cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis        return false;  // Matches something.
7204cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
7214cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBracket();
7224cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
7234cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::r_brace:
7244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      if (BraceCount && !isFirstTokenConsumed)
7254cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis        return false;  // Matches something.
7264cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
7274cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBrace();
7284cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
7294cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
7307d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    case tok::code_completion:
7317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      Toks.push_back(Tok);
7327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      ConsumeCodeCompletionToken();
7337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      break;
7347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7354cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::string_literal:
7364cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::wide_string_literal:
7375cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf8_string_literal:
7385cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf16_string_literal:
7395cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf32_string_literal:
7404cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
7414cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeStringToken();
7424cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
74314b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis    case tok::semi:
74414b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      if (StopAtSemi)
74514b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis        return false;
74614b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      // FALL THROUGH.
7474cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    default:
7484cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // consume this token.
7494cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
7504cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeToken();
7514cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
7524cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    }
7534cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    isFirstTokenConsumed = false;
7544cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
7554cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
7566df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
7576df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl/// \brief Consume tokens and store them in the passed token container until
7586df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl/// we've passed the try keyword and constructor initializers and have consumed
759a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl/// the opening brace of the function body. The opening brace will be consumed
760a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl/// if and only if there was no error.
7616df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl///
76254ca069d1526df84a48a3974871a7c4f46549651Richard Smith/// \return True on error.
763a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redlbool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
7646df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  if (Tok.is(tok::kw_try)) {
7656df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    Toks.push_back(Tok);
7666df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    ConsumeToken();
7676df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  }
7686df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
76954ca069d1526df84a48a3974871a7c4f46549651Richard Smith  if (Tok.isNot(tok::colon)) {
77054ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // Easy case, just a function body.
7716df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
77254ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // Grab any remaining garbage to be diagnosed later. We stop when we reach a
77354ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // brace: an opening one is the function body, while a closing one probably
77454ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // means we've reached the end of the class.
77554ca069d1526df84a48a3974871a7c4f46549651Richard Smith    ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
77654ca069d1526df84a48a3974871a7c4f46549651Richard Smith                         /*StopAtSemi=*/true,
77754ca069d1526df84a48a3974871a7c4f46549651Richard Smith                         /*ConsumeFinalToken=*/false);
77854ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (Tok.isNot(tok::l_brace))
779651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
780e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman
78154ca069d1526df84a48a3974871a7c4f46549651Richard Smith    Toks.push_back(Tok);
78254ca069d1526df84a48a3974871a7c4f46549651Richard Smith    ConsumeBrace();
78354ca069d1526df84a48a3974871a7c4f46549651Richard Smith    return false;
78454ca069d1526df84a48a3974871a7c4f46549651Richard Smith  }
7856df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
78654ca069d1526df84a48a3974871a7c4f46549651Richard Smith  Toks.push_back(Tok);
78754ca069d1526df84a48a3974871a7c4f46549651Richard Smith  ConsumeToken();
78854ca069d1526df84a48a3974871a7c4f46549651Richard Smith
78954ca069d1526df84a48a3974871a7c4f46549651Richard Smith  // We can't reliably skip over a mem-initializer-id, because it could be
79054ca069d1526df84a48a3974871a7c4f46549651Richard Smith  // a template-id involving not-yet-declared names. Given:
79154ca069d1526df84a48a3974871a7c4f46549651Richard Smith  //
79254ca069d1526df84a48a3974871a7c4f46549651Richard Smith  //   S ( ) : a < b < c > ( e )
79354ca069d1526df84a48a3974871a7c4f46549651Richard Smith  //
79454ca069d1526df84a48a3974871a7c4f46549651Richard Smith  // 'e' might be an initializer or part of a template argument, depending
79554ca069d1526df84a48a3974871a7c4f46549651Richard Smith  // on whether 'b' is a template.
79654ca069d1526df84a48a3974871a7c4f46549651Richard Smith
79754ca069d1526df84a48a3974871a7c4f46549651Richard Smith  // Track whether we might be inside a template argument. We can give
79854ca069d1526df84a48a3974871a7c4f46549651Richard Smith  // significantly better diagnostics if we know that we're not.
79954ca069d1526df84a48a3974871a7c4f46549651Richard Smith  bool MightBeTemplateArgument = false;
80054ca069d1526df84a48a3974871a7c4f46549651Richard Smith
80154ca069d1526df84a48a3974871a7c4f46549651Richard Smith  while (true) {
80254ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // Skip over the mem-initializer-id, if possible.
80354ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (Tok.is(tok::kw_decltype)) {
80454ca069d1526df84a48a3974871a7c4f46549651Richard Smith      Toks.push_back(Tok);
80554ca069d1526df84a48a3974871a7c4f46549651Richard Smith      SourceLocation OpenLoc = ConsumeToken();
80654ca069d1526df84a48a3974871a7c4f46549651Richard Smith      if (Tok.isNot(tok::l_paren))
80754ca069d1526df84a48a3974871a7c4f46549651Richard Smith        return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
80854ca069d1526df84a48a3974871a7c4f46549651Richard Smith                 << "decltype";
80954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      Toks.push_back(Tok);
81054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      ConsumeParen();
81154ca069d1526df84a48a3974871a7c4f46549651Richard Smith      if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
812651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
813651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag(OpenLoc, diag::note_matching) << tok::l_paren;
8146df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        return true;
815e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman      }
81654ca069d1526df84a48a3974871a7c4f46549651Richard Smith    }
81754ca069d1526df84a48a3974871a7c4f46549651Richard Smith    do {
81854ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // Walk over a component of a nested-name-specifier.
81954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      if (Tok.is(tok::coloncolon)) {
820e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman        Toks.push_back(Tok);
821e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman        ConsumeToken();
82254ca069d1526df84a48a3974871a7c4f46549651Richard Smith
82354ca069d1526df84a48a3974871a7c4f46549651Richard Smith        if (Tok.is(tok::kw_template)) {
82454ca069d1526df84a48a3974871a7c4f46549651Richard Smith          Toks.push_back(Tok);
82554ca069d1526df84a48a3974871a7c4f46549651Richard Smith          ConsumeToken();
82654ca069d1526df84a48a3974871a7c4f46549651Richard Smith        }
827e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman      }
828a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl
82954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) {
830a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl        Toks.push_back(Tok);
831a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl        ConsumeToken();
83254ca069d1526df84a48a3974871a7c4f46549651Richard Smith      } else if (Tok.is(tok::code_completion)) {
83354ca069d1526df84a48a3974871a7c4f46549651Richard Smith        Toks.push_back(Tok);
83454ca069d1526df84a48a3974871a7c4f46549651Richard Smith        ConsumeCodeCompletionToken();
83554ca069d1526df84a48a3974871a7c4f46549651Richard Smith        // Consume the rest of the initializers permissively.
83654ca069d1526df84a48a3974871a7c4f46549651Richard Smith        // FIXME: We should be able to perform code-completion here even if
83754ca069d1526df84a48a3974871a7c4f46549651Richard Smith        //        there isn't a subsequent '{' token.
83854ca069d1526df84a48a3974871a7c4f46549651Richard Smith        MightBeTemplateArgument = true;
83954ca069d1526df84a48a3974871a7c4f46549651Richard Smith        break;
84054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      } else {
841e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman        break;
842a891a32d3762ee641a29c091d286f2a7432671a5Sebastian Redl      }
84354ca069d1526df84a48a3974871a7c4f46549651Richard Smith    } while (Tok.is(tok::coloncolon));
84454ca069d1526df84a48a3974871a7c4f46549651Richard Smith
84554ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (Tok.is(tok::less))
84654ca069d1526df84a48a3974871a7c4f46549651Richard Smith      MightBeTemplateArgument = true;
84754ca069d1526df84a48a3974871a7c4f46549651Richard Smith
84854ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (MightBeTemplateArgument) {
84954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // We may be inside a template argument list. Grab up to the start of the
85054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // next parenthesized initializer or braced-init-list. This *might* be the
85154ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // initializer, or it might be a subexpression in the template argument
85254ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // list.
85354ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // FIXME: Count angle brackets, and clear MightBeTemplateArgument
85454ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //        if all angles are closed.
85554ca069d1526df84a48a3974871a7c4f46549651Richard Smith      if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
85654ca069d1526df84a48a3974871a7c4f46549651Richard Smith                                /*StopAtSemi=*/true,
85754ca069d1526df84a48a3974871a7c4f46549651Richard Smith                                /*ConsumeFinalToken=*/false)) {
85854ca069d1526df84a48a3974871a7c4f46549651Richard Smith        // We're not just missing the initializer, we're also missing the
85954ca069d1526df84a48a3974871a7c4f46549651Richard Smith        // function body!
860651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
86154ca069d1526df84a48a3974871a7c4f46549651Richard Smith      }
86254ca069d1526df84a48a3974871a7c4f46549651Richard Smith    } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
86354ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // We found something weird in a mem-initializer-id.
864651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (getLangOpts().CPlusPlus11)
865651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return Diag(Tok.getLocation(), diag::err_expected_either)
866651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines               << tok::l_paren << tok::l_brace;
867651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      else
868651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
86954ca069d1526df84a48a3974871a7c4f46549651Richard Smith    }
87054ca069d1526df84a48a3974871a7c4f46549651Richard Smith
87154ca069d1526df84a48a3974871a7c4f46549651Richard Smith    tok::TokenKind kind = Tok.getKind();
87254ca069d1526df84a48a3974871a7c4f46549651Richard Smith    Toks.push_back(Tok);
87354ca069d1526df84a48a3974871a7c4f46549651Richard Smith    bool IsLParen = (kind == tok::l_paren);
87454ca069d1526df84a48a3974871a7c4f46549651Richard Smith    SourceLocation OpenLoc = Tok.getLocation();
87554ca069d1526df84a48a3974871a7c4f46549651Richard Smith
87654ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (IsLParen) {
87754ca069d1526df84a48a3974871a7c4f46549651Richard Smith      ConsumeParen();
87854ca069d1526df84a48a3974871a7c4f46549651Richard Smith    } else {
87954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      assert(kind == tok::l_brace && "Must be left paren or brace here.");
88054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      ConsumeBrace();
88154ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // In C++03, this has to be the start of the function body, which
88254ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // means the initializer is malformed; we'll diagnose it later.
88354ca069d1526df84a48a3974871a7c4f46549651Richard Smith      if (!getLangOpts().CPlusPlus11)
88454ca069d1526df84a48a3974871a7c4f46549651Richard Smith        return false;
88554ca069d1526df84a48a3974871a7c4f46549651Richard Smith    }
88654ca069d1526df84a48a3974871a7c4f46549651Richard Smith
88754ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // Grab the initializer (or the subexpression of the template argument).
88854ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
88954ca069d1526df84a48a3974871a7c4f46549651Richard Smith    //        if we might be inside the braces of a lambda-expression.
890651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
891651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
892651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(Tok, diag::err_expected) << CloseKind;
893651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(OpenLoc, diag::note_matching) << kind;
89454ca069d1526df84a48a3974871a7c4f46549651Richard Smith      return true;
8956df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    }
8966df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
89754ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // Grab pack ellipsis, if present.
89854ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (Tok.is(tok::ellipsis)) {
89954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      Toks.push_back(Tok);
90054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      ConsumeToken();
90154ca069d1526df84a48a3974871a7c4f46549651Richard Smith    }
90254ca069d1526df84a48a3974871a7c4f46549651Richard Smith
90354ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // If we know we just consumed a mem-initializer, we must have ',' or '{'
90454ca069d1526df84a48a3974871a7c4f46549651Richard Smith    // next.
90554ca069d1526df84a48a3974871a7c4f46549651Richard Smith    if (Tok.is(tok::comma)) {
90654ca069d1526df84a48a3974871a7c4f46549651Richard Smith      Toks.push_back(Tok);
90754ca069d1526df84a48a3974871a7c4f46549651Richard Smith      ConsumeToken();
90854ca069d1526df84a48a3974871a7c4f46549651Richard Smith    } else if (Tok.is(tok::l_brace)) {
90954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // This is the function body if the ')' or '}' is immediately followed by
91054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // a '{'. That cannot happen within a template argument, apart from the
91154ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // case where a template argument contains a compound literal:
91254ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //
91354ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //   S ( ) : a < b < c > ( d ) { }
91454ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //   // End of declaration, or still inside the template argument?
91554ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //
91654ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // ... and the case where the template argument contains a lambda:
91754ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //
91854ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //   S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
91954ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //     ( ) > ( ) { }
92054ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //
92154ca069d1526df84a48a3974871a7c4f46549651Richard Smith      // FIXME: Disambiguate these cases. Note that the latter case is probably
92254ca069d1526df84a48a3974871a7c4f46549651Richard Smith      //        going to be made ill-formed by core issue 1607.
92354ca069d1526df84a48a3974871a7c4f46549651Richard Smith      Toks.push_back(Tok);
92454ca069d1526df84a48a3974871a7c4f46549651Richard Smith      ConsumeBrace();
92554ca069d1526df84a48a3974871a7c4f46549651Richard Smith      return false;
92654ca069d1526df84a48a3974871a7c4f46549651Richard Smith    } else if (!MightBeTemplateArgument) {
927651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
928651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                                << tok::comma;
92954ca069d1526df84a48a3974871a7c4f46549651Richard Smith    }
930e9ee382c32a83e9807a2fe4cfd52b5a11169a4b8Eli Friedman  }
9316df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl}
9329bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9339bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith/// \brief Consume and store tokens from the '?' to the ':' in a conditional
9349bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith/// expression.
9359bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smithbool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
9369bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  // Consume '?'.
9379bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  assert(Tok.is(tok::question));
9389bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  Toks.push_back(Tok);
9399bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  ConsumeToken();
9409bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9419bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  while (Tok.isNot(tok::colon)) {
9426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
9436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                              /*StopAtSemi=*/true,
9446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                              /*ConsumeFinalToken=*/false))
9459bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      return false;
9469bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9479bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // If we found a nested conditional, consume it.
9489bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
9499bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      return false;
9509bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  }
9519bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9529bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  // Consume ':'.
9539bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  Toks.push_back(Tok);
9549bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  ConsumeToken();
9559bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  return true;
9569bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith}
9579bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9589bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith/// \brief A tentative parsing action that can also revert token annotations.
9599bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smithclass Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
9609bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smithpublic:
9619bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  explicit UnannotatedTentativeParsingAction(Parser &Self,
9629bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith                                             tok::TokenKind EndKind)
9639bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
9649bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // Stash away the old token stream, so we can restore it once the
9659bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // tentative parse is complete.
9669bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    TentativeParsingAction Inner(Self);
9679bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
9689bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    Inner.Revert();
9699bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  }
9709bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9719bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  void RevertAnnotations() {
9729bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    Revert();
9739bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9749bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // Put back the original tokens.
9758fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
9769bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    if (Toks.size()) {
9779bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Token *Buffer = new Token[Toks.size()];
9789bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      std::copy(Toks.begin() + 1, Toks.end(), Buffer);
9799bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Buffer[Toks.size() - 1] = Self.Tok;
9809bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true);
9819bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9829bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Self.Tok = Toks.front();
9839bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    }
9849bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  }
9859bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9869bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smithprivate:
9879bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  Parser &Self;
9889bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  CachedTokens Toks;
9899bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  tok::TokenKind EndKind;
9909bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith};
9919bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
9929bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith/// ConsumeAndStoreInitializer - Consume and store the token at the passed token
9939bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith/// container until the end of the current initializer expression (either a
9949bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith/// default argument or an in-class initializer for a non-static data member).
995176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines///
996176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// Returns \c true if we reached the end of something initializer-shaped,
997176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// \c false if we bailed out.
9989bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smithbool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
9999bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith                                        CachedInitKind CIK) {
10009bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  // We always want this function to consume at least one token if not at EOF.
1001176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  bool IsFirstToken = true;
10029bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10039bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  // Number of possible unclosed <s we've seen so far. These might be templates,
10049bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  // and might not, but if there were none of them (or we know for sure that
10059bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  // we're within a template), we can avoid a tentative parse.
10069bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  unsigned AngleCount = 0;
10079bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  unsigned KnownTemplateCount = 0;
10089bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10099bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  while (1) {
10109bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    switch (Tok.getKind()) {
10119bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::comma:
10129bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // If we might be in a template, perform a tentative parse to check.
10139bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (!AngleCount)
10149bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        // Not a template argument: this is the end of the initializer.
10159bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        return true;
10169bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (KnownTemplateCount)
10179bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        goto consume_token;
10189bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10199bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // We hit a comma inside angle brackets. This is the hard case. The
10209bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // rule we follow is:
10219bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //  * For a default argument, if the tokens after the comma form a
10229bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //    syntactically-valid parameter-declaration-clause, in which each
10239bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //    parameter has an initializer, then this comma ends the default
10249bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //    argument.
10259bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //  * For a default initializer, if the tokens after the comma form a
10269bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //    syntactically-valid init-declarator-list, then this comma ends
10279bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      //    the default initializer.
10289bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      {
10299bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        UnannotatedTentativeParsingAction PA(*this,
10309bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith                                             CIK == CIK_DefaultInitializer
10319bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith                                               ? tok::semi : tok::r_paren);
10329bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        Sema::TentativeAnalysisScope Scope(Actions);
10339bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        TPResult Result = TPResult::Error;
10359bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        ConsumeToken();
10369bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        switch (CIK) {
10379bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        case CIK_DefaultInitializer:
10389bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          Result = TryParseInitDeclaratorList();
10399bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // If we parsed a complete, ambiguous init-declarator-list, this
10409bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // is only syntactically-valid if it's followed by a semicolon.
10416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
10426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            Result = TPResult::False;
10439bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          break;
10449bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10459bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        case CIK_DefaultArgument:
10469bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          bool InvalidAsDeclaration = false;
10479bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          Result = TryParseParameterDeclarationClause(
10480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines              &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
10499bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // If this is an expression or a declaration with a missing
10509bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          // 'typename', assume it's not a declaration.
10516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
10526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            Result = TPResult::False;
10539bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          break;
10549bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        }
10559bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10569bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        // If what follows could be a declaration, it is a declaration.
10576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Result != TPResult::False && Result != TPResult::Error) {
10589bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          PA.Revert();
10599bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          return true;
10609bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        }
10619bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10629bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        // In the uncommon case that we decide the following tokens are part
10639bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        // of a template argument, revert any annotations we've performed in
10649bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        // those tokens. We're not going to look them up until we've parsed
10659bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        // the rest of the class, and that might add more declarations.
10669bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        PA.RevertAnnotations();
10679bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      }
10689bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10699bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Keep going. We know we're inside a template argument list now.
10709bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ++KnownTemplateCount;
10719bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      goto consume_token;
10729bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10739bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::eof:
1074651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_begin:
1075651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_end:
1076651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_include:
10779bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Ran out of tokens.
10789bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      return false;
10799bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10809bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::less:
10819bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // FIXME: A '<' can only start a template-id if it's preceded by an
10829bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // identifier, an operator-function-id, or a literal-operator-id.
10839bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ++AngleCount;
10849bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      goto consume_token;
10859bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10869bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::question:
10879bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
10889bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // that is *never* the end of the initializer. Skip to the ':'.
10899bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (!ConsumeAndStoreConditional(Toks))
10909bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        return false;
10919bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
10929bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
10939bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::greatergreatergreater:
10949bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (!getLangOpts().CPlusPlus11)
10959bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        goto consume_token;
10969bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (AngleCount) --AngleCount;
10979bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (KnownTemplateCount) --KnownTemplateCount;
10989bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Fall through.
10999bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::greatergreater:
11009bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (!getLangOpts().CPlusPlus11)
11019bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        goto consume_token;
11029bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (AngleCount) --AngleCount;
11039bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (KnownTemplateCount) --KnownTemplateCount;
11049bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Fall through.
11059bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::greater:
11069bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (AngleCount) --AngleCount;
11079bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (KnownTemplateCount) --KnownTemplateCount;
11089bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      goto consume_token;
11099bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
11109bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::kw_template:
11119bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // 'template' identifier '<' is known to start a template argument list,
11129bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // and can be used to disambiguate the parse.
11139bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // FIXME: Support all forms of 'template' unqualified-id '<'.
11149bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
11159bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeToken();
11169bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (Tok.is(tok::identifier)) {
11179bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        Toks.push_back(Tok);
11189bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        ConsumeToken();
11199bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        if (Tok.is(tok::less)) {
1120176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          ++AngleCount;
11219bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          ++KnownTemplateCount;
11229bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          Toks.push_back(Tok);
11239bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith          ConsumeToken();
11249bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        }
11259bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      }
11269bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
11279bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
11289bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::kw_operator:
11299bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // If 'operator' precedes other punctuation, that punctuation loses
11309bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // its special behavior.
11319bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
11329bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeToken();
11339bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      switch (Tok.getKind()) {
11349bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      case tok::comma:
11359bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      case tok::greatergreatergreater:
11369bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      case tok::greatergreater:
11379bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      case tok::greater:
11389bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      case tok::less:
11399bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        Toks.push_back(Tok);
11409bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        ConsumeToken();
11419bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        break;
11429bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      default:
11439bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        break;
11449bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      }
11459bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
11469bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
11479bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::l_paren:
11489bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Recursively consume properly-nested parens.
11499bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
11509bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeParen();
11519bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
11529bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
11539bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::l_square:
11549bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Recursively consume properly-nested square brackets.
11559bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
11569bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeBracket();
11579bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
11589bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
11599bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::l_brace:
11609bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // Recursively consume properly-nested braces.
11619bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
11629bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeBrace();
11639bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
11649bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
11659bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
11669bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
11679bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // Since the user wasn't looking for this token (if they were, it would
11689bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // already be handled), this isn't balanced.  If there is a LHS token at a
11699bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    // higher level, we will assume that this matches the unbalanced token
1170176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // and return it.  Otherwise, this is a spurious RHS token, which we
1171176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // consume and pass on to downstream code to diagnose.
11729bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::r_paren:
11739bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (CIK == CIK_DefaultArgument)
11749bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        return true; // End of the default argument.
1175176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (ParenCount && !IsFirstToken)
1176176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return false;
1177176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Toks.push_back(Tok);
1178176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ConsumeParen();
1179176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      continue;
11809bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::r_square:
1181176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (BracketCount && !IsFirstToken)
1182176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return false;
1183176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Toks.push_back(Tok);
1184176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ConsumeBracket();
1185176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      continue;
11869bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::r_brace:
1187176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (BraceCount && !IsFirstToken)
1188176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return false;
1189176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Toks.push_back(Tok);
1190176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ConsumeBrace();
1191176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      continue;
11929bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
11939bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::code_completion:
11949bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
11959bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeCodeCompletionToken();
11969bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
11979bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith
11989bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::string_literal:
11999bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::wide_string_literal:
12009bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::utf8_string_literal:
12019bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::utf16_string_literal:
12029bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::utf32_string_literal:
12039bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
12049bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeStringToken();
12059bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
12069bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    case tok::semi:
12079bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      if (CIK == CIK_DefaultInitializer)
12089bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith        return true; // End of the default initializer.
12099bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      // FALL THROUGH.
12109bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    default:
12119bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    consume_token:
12129bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      Toks.push_back(Tok);
12139bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      ConsumeToken();
12149bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith      break;
12159bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    }
1216176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    IsFirstToken = false;
12179bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith  }
12189bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith}
1219