ParseCXXInlineMethods.cpp revision 7a614d8380297fcd2bc23986241905d97222948c
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
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
154cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis#include "clang/Parse/Parser.h"
1619510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
188387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet#include "clang/AST/DeclTemplate.h"
194cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidisusing namespace clang;
204cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
21d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl/// ParseCXXInlineMethodDef - We parsed and verified that the specified
224cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// Declarator is a well formed C++ inline method definition. Now lex its body
234cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// and store its tokens for parsing after the C++ class is complete.
24eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCallDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D,
254867347e82648d3baf09524b98b09c297a5a198fNico Weber                                const ParsedTemplateInfo &TemplateInfo,
266a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet                                const VirtSpecifiers& VS, ExprResult& Init) {
27075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
28e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
29e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt          Tok.is(tok::equal)) &&
30e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt         "Current token not a '{', ':', '=', or 'try'!");
314cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
32f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  MultiTemplateParamsArg TemplateParams(Actions,
334cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt          TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
344cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt          TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
354cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt
36d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *FnD;
3767d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall  if (D.getDeclSpec().isFriendSpecified())
3837b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor    // FIXME: Friend templates
3923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
404cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt                                          move(TemplateParams));
414867347e82648d3baf09524b98b09c297a5a198fNico Weber  else { // FIXME: pass template information through
4223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
4369a87357310260c4b2c5dce2cdcd10c3fd3a0a58Anders Carlsson                                           move(TemplateParams), 0,
446a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet                                           VS, Init.release(),
457a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*HasInit=*/false,
466a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet                                           /*IsDefinition*/true);
474867347e82648d3baf09524b98b09c297a5a198fNico Weber  }
484cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
49d33133cdc1af466f9c276249b2621be03867888bEli Friedman  HandleMemberFunctionDefaultArgs(D, FnD);
50d33133cdc1af466f9c276249b2621be03867888bEli Friedman
51eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  D.complete(FnD);
52eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
53e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  if (Tok.is(tok::equal)) {
54e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    ConsumeToken();
55e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
56e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    bool Delete = false;
57e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    SourceLocation KWLoc;
58e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (Tok.is(tok::kw_delete)) {
59e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      if (!getLang().CPlusPlus0x)
60e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
61e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
62e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      KWLoc = ConsumeToken();
63e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Actions.SetDeclDeleted(FnD, KWLoc);
64e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Delete = true;
65e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else if (Tok.is(tok::kw_default)) {
66e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      if (!getLang().CPlusPlus0x)
67e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
68e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
69e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      KWLoc = ConsumeToken();
70e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Actions.SetDeclDefaulted(FnD, KWLoc);
71e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else {
72e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
73e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
74e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
75e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (Tok.is(tok::comma)) {
76e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
77e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        << Delete;
78e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      SkipUntil(tok::semi);
79e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else {
80e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
81e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                       Delete ? "delete" : "default", tok::semi);
82e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
83e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
84e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return FnD;
85e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
86e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
878387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, if we are within a class template
888387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // or if we are about to parse function member template then consume
898387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // the tokens and store them for parsing at the end of the translation unit.
908387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  if (getLang().DelayedTemplateParsing &&
918387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      ((Actions.CurContext->isDependentContext() ||
928387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
938387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
948387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        !D.getDeclSpec().isFriendSpecified()) {
958387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
968387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (FnD) {
978387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplatedFunction *LPT =
988387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        new LateParsedTemplatedFunction(this, FnD);
998387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
1008387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FD = 0;
1018387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
1028387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FD = FunTmpl->getTemplatedDecl();
1038387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
1048387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FD = cast<FunctionDecl>(FnD);
10581542fd91bd5e7e65ebae3eaad117bdaeaf7d737Chandler Carruth      Actions.CheckForFunctionRedefinition(FD);
1068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
1078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FD] = LPT;
1088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FD);
1098387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
1108387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
1118387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
1128387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
1138387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
1148387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
1158387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return FnD;
1168387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
1178387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
1184cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // Consume the tokens and store them for later parsing.
1194cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
120d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  LexedMethod* LM = new LexedMethod(this, FnD);
121d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  getCurrentClass().LateParsedDeclarations.push_back(LM);
122d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  LM->TemplateScope = getCurScope()->isTemplateParamScope();
123d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  CachedTokens &Toks = LM->Toks;
1244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
125d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  tok::TokenKind kind = Tok.getKind();
126d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  // We may have a constructor initializer or function-try-block here.
127d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (kind == tok::colon || kind == tok::kw_try) {
1287ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    // Consume everything up to (and including) the left brace.
12914b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis    if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
1303f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor      // We didn't find the left-brace we expected after the
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // constructor initializer.
1323f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor      if (Tok.is(tok::semi)) {
1333f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor        // We found a semicolon; complain, consume the semicolon, and
1343f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor        // don't try to parse this method later.
1353f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor        Diag(Tok.getLocation(), diag::err_expected_lbrace);
1363f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor        ConsumeAnyToken();
137d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor        delete getCurrentClass().LateParsedDeclarations.back();
138d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor        getCurrentClass().LateParsedDeclarations.pop_back();
1393f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor        return FnD;
1403f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor      }
1413f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor    }
1423f08d181f620e6bf4971c436fc9878f98a02bbe3Douglas Gregor
1437ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  } else {
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Begin by storing the '{' token.
1457ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    Toks.push_back(Tok);
1467ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ConsumeBrace();
1477ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  }
1487ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // Consume everything up to (and including) the matching right brace.
14914b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1504cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
151d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  // If we're in a function-try-block, we need to store all the catch blocks.
152d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (kind == tok::kw_try) {
153d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    while (Tok.is(tok::kw_catch)) {
15414b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
15514b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
156d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    }
157d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  }
158d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
15987f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor
16087f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor  if (!FnD) {
16187f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    // If semantic analysis could not build a function declaration,
16287f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    // just throw away the late-parsed declaration.
16387f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    delete getCurrentClass().LateParsedDeclarations.back();
16487f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor    getCurrentClass().LateParsedDeclarations.pop_back();
16587f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor  }
16687f106462ce49a4a9b812b9de92aadd4e54567bdDouglas Gregor
1674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  return FnD;
1684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
1694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
1707a614d8380297fcd2bc23986241905d97222948cRichard Smith/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
1717a614d8380297fcd2bc23986241905d97222948cRichard Smith/// specified Declarator is a well formed C++ non-static data member
1727a614d8380297fcd2bc23986241905d97222948cRichard Smith/// declaration. Now lex its initializer and store its tokens for parsing
1737a614d8380297fcd2bc23986241905d97222948cRichard Smith/// after the class is complete.
1747a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
1757a614d8380297fcd2bc23986241905d97222948cRichard Smith  assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
1767a614d8380297fcd2bc23986241905d97222948cRichard Smith         "Current token not a '{' or '='!");
1777a614d8380297fcd2bc23986241905d97222948cRichard Smith
1787a614d8380297fcd2bc23986241905d97222948cRichard Smith  LateParsedMemberInitializer *MI =
1797a614d8380297fcd2bc23986241905d97222948cRichard Smith    new LateParsedMemberInitializer(this, VarD);
1807a614d8380297fcd2bc23986241905d97222948cRichard Smith  getCurrentClass().LateParsedDeclarations.push_back(MI);
1817a614d8380297fcd2bc23986241905d97222948cRichard Smith  CachedTokens &Toks = MI->Toks;
1827a614d8380297fcd2bc23986241905d97222948cRichard Smith
1837a614d8380297fcd2bc23986241905d97222948cRichard Smith  tok::TokenKind kind = Tok.getKind();
1847a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (kind == tok::equal) {
1857a614d8380297fcd2bc23986241905d97222948cRichard Smith    Toks.push_back(Tok);
1867a614d8380297fcd2bc23986241905d97222948cRichard Smith    ConsumeAnyToken();
1877a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
1887a614d8380297fcd2bc23986241905d97222948cRichard Smith
1897a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (kind == tok::l_brace) {
1907a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Begin by storing the '{' token.
1917a614d8380297fcd2bc23986241905d97222948cRichard Smith    Toks.push_back(Tok);
1927a614d8380297fcd2bc23986241905d97222948cRichard Smith    ConsumeBrace();
1937a614d8380297fcd2bc23986241905d97222948cRichard Smith
1947a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Consume everything up to (and including) the matching right brace.
1957a614d8380297fcd2bc23986241905d97222948cRichard Smith    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
1967a614d8380297fcd2bc23986241905d97222948cRichard Smith  } else {
1977a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Consume everything up to (but excluding) the comma or semicolon.
1987a614d8380297fcd2bc23986241905d97222948cRichard Smith    ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
1997a614d8380297fcd2bc23986241905d97222948cRichard Smith                         /*ConsumeFinalToken=*/false);
2007a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
2017a614d8380297fcd2bc23986241905d97222948cRichard Smith
2027a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Store an artificial EOF token to ensure that we don't run off the end of
2037a614d8380297fcd2bc23986241905d97222948cRichard Smith  // the initializer when we come to parse it.
2047a614d8380297fcd2bc23986241905d97222948cRichard Smith  Token Eof;
2057a614d8380297fcd2bc23986241905d97222948cRichard Smith  Eof.startToken();
2067a614d8380297fcd2bc23986241905d97222948cRichard Smith  Eof.setKind(tok::eof);
2077a614d8380297fcd2bc23986241905d97222948cRichard Smith  Eof.setLocation(Tok.getLocation());
2087a614d8380297fcd2bc23986241905d97222948cRichard Smith  Toks.push_back(Eof);
2097a614d8380297fcd2bc23986241905d97222948cRichard Smith}
2107a614d8380297fcd2bc23986241905d97222948cRichard Smith
211d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas GregorParser::LateParsedDeclaration::~LateParsedDeclaration() {}
212d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
2137a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
214d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
215d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
216d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas GregorParser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
217d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  : Self(P), Class(C) {}
218d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
219d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas GregorParser::LateParsedClass::~LateParsedClass() {
220d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->DeallocateParsedClasses(Class);
221d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
222d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
223d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedClass::ParseLexedMethodDeclarations() {
224d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDeclarations(*Class);
225d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
226d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
2277a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::LateParsedClass::ParseLexedMemberInitializers() {
2287a614d8380297fcd2bc23986241905d97222948cRichard Smith  Self->ParseLexedMemberInitializers(*Class);
2297a614d8380297fcd2bc23986241905d97222948cRichard Smith}
2307a614d8380297fcd2bc23986241905d97222948cRichard Smith
231d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedClass::ParseLexedMethodDefs() {
232d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDefs(*Class);
233d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
234d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
235d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
236d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDeclaration(*this);
237d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
238d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
239d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::LexedMethod::ParseLexedMethodDefs() {
240d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Self->ParseLexedMethodDef(*this);
241d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
242d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
2437a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
2447a614d8380297fcd2bc23986241905d97222948cRichard Smith  Self->ParseLexedMemberInitializer(*this);
2457a614d8380297fcd2bc23986241905d97222948cRichard Smith}
2467a614d8380297fcd2bc23986241905d97222948cRichard Smith
24772b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// ParseLexedMethodDeclarations - We finished parsing the member
24872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// specification of a top (non-nested) C++ class. Now go over the
24972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// stack of method declarations with some parts for which parsing was
25072b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// delayed (such as default arguments) and parse them.
2516569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
2526569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
253d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
2546569d68745c8213709740337d2be52b031384f58Douglas Gregor  if (HasTemplateScope)
25523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
2566569d68745c8213709740337d2be52b031384f58Douglas Gregor
2577a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // The current scope is still active if we're the top-level class.
2587a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // Otherwise we'll need to push and enter a new scope.
2596569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasClassScope = !Class.TopLevelClass;
2604cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt  ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
2614cd8494d9a2f99cbf38147bca80be18cdff83734Sean Hunt                        HasClassScope);
2627a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (HasClassScope)
26323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
2646569d68745c8213709740337d2be52b031384f58Douglas Gregor
265d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
266d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
267d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  }
26872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
269d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (HasClassScope)
270d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
271d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
2727fd3a6456a88cdd225b92ae1606144f24c7f51d4Argyrios Kyrtzidis
273d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
274d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // If this is a member template, introduce the template parameter scope.
275d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
276d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (LM.TemplateScope)
277d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
278d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
279d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Start the delayed C++ method declaration
280d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
281d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
282d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Introduce the parameters into scope and parse their default
283d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // arguments.
284d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope PrototypeScope(this,
285d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor                            Scope::FunctionPrototypeScope|Scope::DeclScope);
286d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
287d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Introduce the parameter into scope.
288d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
289d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
290d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
291d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Save the current token position.
292d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      SourceLocation origLoc = Tok.getLocation();
293d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
294d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Parse the default argument from its saved token stream.
295d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      Toks->push_back(Tok); // So that the current token doesn't get lost
296d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
297d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
298d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Consume the previously-pushed token.
299d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      ConsumeAnyToken();
300d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
301d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Consume the '='.
302d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      assert(Tok.is(tok::equal) && "Default argument not starting with '='");
303d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      SourceLocation EqualLoc = ConsumeToken();
304d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
305d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // The argument isn't actually potentially evaluated unless it is
306d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // used.
307d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      EnterExpressionEvaluationContext Eval(Actions,
308d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor                                            Sema::PotentiallyEvaluatedIfUsed);
309d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
310d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      ExprResult DefArgResult(ParseAssignmentExpression());
311d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      if (DefArgResult.isInvalid())
312d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor        Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
313d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      else {
314d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor        if (Tok.is(tok::cxx_defaultarg_end))
315d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor          ConsumeToken();
316d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor        else
317d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor          Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
318d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor        Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
319d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor                                          DefArgResult.take());
320d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      }
32172b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
322d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
323d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor                                                         Tok.getLocation()) &&
324d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor             "ParseAssignmentExpression went over the default arg tokens!");
325d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // There could be leftover tokens (e.g. because of an error).
326d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      // Skip through until we reach the original token position.
327d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
32872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor        ConsumeAnyToken();
32972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
330d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      delete Toks;
331d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      LM.DefaultArgs[I].Toks = 0;
33272b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor    }
33372b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor  }
334d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  PrototypeScope.Exit();
3356569d68745c8213709740337d2be52b031384f58Douglas Gregor
336d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Finish the delayed C++ method declaration.
337d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
33872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor}
33972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor
3404cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseLexedMethodDefs - We finished parsing the member specification of a top
3414cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// (non-nested) C++ class. Now go over the stack of lexed methods that were
3424cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// collected during its parsing and parse them all.
3436569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::ParseLexedMethodDefs(ParsingClass &Class) {
3446569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
345d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
3466569d68745c8213709740337d2be52b031384f58Douglas Gregor  if (HasTemplateScope)
34723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
3486569d68745c8213709740337d2be52b031384f58Douglas Gregor
3496569d68745c8213709740337d2be52b031384f58Douglas Gregor  bool HasClassScope = !Class.TopLevelClass;
3506569d68745c8213709740337d2be52b031384f58Douglas Gregor  ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
3516569d68745c8213709740337d2be52b031384f58Douglas Gregor                        HasClassScope);
3526569d68745c8213709740337d2be52b031384f58Douglas Gregor
353d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
354d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
355d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  }
356d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor}
3574cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
358d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregorvoid Parser::ParseLexedMethodDef(LexedMethod &LM) {
359d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // If this is a member template, introduce the template parameter scope.
360d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
361d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (LM.TemplateScope)
362d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
363d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
364d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Save the current token position.
365d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  SourceLocation origLoc = Tok.getLocation();
366d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
367d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  assert(!LM.Toks.empty() && "Empty body!");
368d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Append the current token at the end of the new token stream so that it
369d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // doesn't get lost.
370d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  LM.Toks.push_back(Tok);
371d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
372d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
373d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Consume the previously pushed token.
374d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ConsumeAnyToken();
375d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
376d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor         && "Inline method not starting with '{', ':' or 'try'");
377d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
378d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // Parse the method body. Function body parsing code is similar enough
379d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  // to be re-used for method bodies as well.
380d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
381d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
382d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
383d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (Tok.is(tok::kw_try)) {
384c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    ParseFunctionTryBlock(LM.D, FnScope);
385d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
386d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor                                                         Tok.getLocation()) &&
387d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor           "ParseFunctionTryBlock went over the cached tokens!");
388d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // There could be leftover tokens (e.g. because of an error).
389d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Skip through until we reach the original token position.
390d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
391d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      ConsumeAnyToken();
392d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    return;
393d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  }
394d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (Tok.is(tok::colon)) {
395d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    ParseConstructorInitializer(LM.D);
3964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
397d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Error recovery.
398d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    if (!Tok.is(tok::l_brace)) {
399c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      FnScope.Exit();
400d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      Actions.ActOnFinishFunctionBody(LM.D, 0);
401d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor      return;
402d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    }
403d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  } else
404d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    Actions.ActOnDefaultCtorInitializers(LM.D);
405d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
406c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  ParseFunctionStatementBody(LM.D, FnScope);
407d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor
408d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (Tok.getLocation() != origLoc) {
409d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Due to parsing error, we either went over the cached tokens or
410d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // there are still cached tokens left. If it's the latter case skip the
411d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // leftover tokens.
412d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // Since this is an uncommon situation that should be avoided, use the
413d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    // expensive isBeforeInTranslationUnit call.
414d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
415d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor                                                        origLoc))
4168f9359f5ae1227f3b489d1d261225d8180b64ed3Argyrios Kyrtzidis      while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
4177558cd00a5ac620990174f3cba86aea4bd9a000aArgyrios Kyrtzidis        ConsumeAnyToken();
4187a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
4197a614d8380297fcd2bc23986241905d97222948cRichard Smith}
4207a614d8380297fcd2bc23986241905d97222948cRichard Smith
4217a614d8380297fcd2bc23986241905d97222948cRichard Smith/// ParseLexedMemberInitializers - We finished parsing the member specification
4227a614d8380297fcd2bc23986241905d97222948cRichard Smith/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
4237a614d8380297fcd2bc23986241905d97222948cRichard Smith/// initializers that were collected during its parsing and parse them all.
4247a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
4257a614d8380297fcd2bc23986241905d97222948cRichard Smith  bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
4267a614d8380297fcd2bc23986241905d97222948cRichard Smith  ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
4277a614d8380297fcd2bc23986241905d97222948cRichard Smith                                HasTemplateScope);
4287a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (HasTemplateScope)
4297a614d8380297fcd2bc23986241905d97222948cRichard Smith    Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
4307a614d8380297fcd2bc23986241905d97222948cRichard Smith
4317a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Set or update the scope flags to include Scope::ThisScope.
4327a614d8380297fcd2bc23986241905d97222948cRichard Smith  bool AlreadyHasClassScope = Class.TopLevelClass;
4337a614d8380297fcd2bc23986241905d97222948cRichard Smith  unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
4347a614d8380297fcd2bc23986241905d97222948cRichard Smith  ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
4357a614d8380297fcd2bc23986241905d97222948cRichard Smith  ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
4367558cd00a5ac620990174f3cba86aea4bd9a000aArgyrios Kyrtzidis
4377a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (!AlreadyHasClassScope)
4387a614d8380297fcd2bc23986241905d97222948cRichard Smith    Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
4397a614d8380297fcd2bc23986241905d97222948cRichard Smith                                                Class.TagOrTemplate);
4407a614d8380297fcd2bc23986241905d97222948cRichard Smith
4417a614d8380297fcd2bc23986241905d97222948cRichard Smith  for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
4427a614d8380297fcd2bc23986241905d97222948cRichard Smith    Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
4434cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
4447a614d8380297fcd2bc23986241905d97222948cRichard Smith
4457a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (!AlreadyHasClassScope)
4467a614d8380297fcd2bc23986241905d97222948cRichard Smith    Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
4477a614d8380297fcd2bc23986241905d97222948cRichard Smith                                                 Class.TagOrTemplate);
4487a614d8380297fcd2bc23986241905d97222948cRichard Smith
4497a614d8380297fcd2bc23986241905d97222948cRichard Smith  Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
4507a614d8380297fcd2bc23986241905d97222948cRichard Smith}
4517a614d8380297fcd2bc23986241905d97222948cRichard Smith
4527a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
4537a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (MI.Field->isInvalidDecl())
4547a614d8380297fcd2bc23986241905d97222948cRichard Smith    return;
4557a614d8380297fcd2bc23986241905d97222948cRichard Smith
4567a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Append the current token at the end of the new token stream so that it
4577a614d8380297fcd2bc23986241905d97222948cRichard Smith  // doesn't get lost.
4587a614d8380297fcd2bc23986241905d97222948cRichard Smith  MI.Toks.push_back(Tok);
4597a614d8380297fcd2bc23986241905d97222948cRichard Smith  PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
4607a614d8380297fcd2bc23986241905d97222948cRichard Smith
4617a614d8380297fcd2bc23986241905d97222948cRichard Smith  // Consume the previously pushed token.
4627a614d8380297fcd2bc23986241905d97222948cRichard Smith  ConsumeAnyToken();
4637a614d8380297fcd2bc23986241905d97222948cRichard Smith
4647a614d8380297fcd2bc23986241905d97222948cRichard Smith  SourceLocation EqualLoc;
4657a614d8380297fcd2bc23986241905d97222948cRichard Smith  ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
4667a614d8380297fcd2bc23986241905d97222948cRichard Smith
4677a614d8380297fcd2bc23986241905d97222948cRichard Smith  Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
4687a614d8380297fcd2bc23986241905d97222948cRichard Smith
4697a614d8380297fcd2bc23986241905d97222948cRichard Smith  // The next token should be our artificial terminating EOF token.
4707a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (Tok.isNot(tok::eof)) {
4717a614d8380297fcd2bc23986241905d97222948cRichard Smith    SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
4727a614d8380297fcd2bc23986241905d97222948cRichard Smith    if (!EndLoc.isValid())
4737a614d8380297fcd2bc23986241905d97222948cRichard Smith      EndLoc = Tok.getLocation();
4747a614d8380297fcd2bc23986241905d97222948cRichard Smith    // No fixit; we can't recover as if there were a semicolon here.
4757a614d8380297fcd2bc23986241905d97222948cRichard Smith    Diag(EndLoc, diag::err_expected_semi_decl_list);
4767a614d8380297fcd2bc23986241905d97222948cRichard Smith
4777a614d8380297fcd2bc23986241905d97222948cRichard Smith    // Consume tokens until we hit the artificial EOF.
4787a614d8380297fcd2bc23986241905d97222948cRichard Smith    while (Tok.isNot(tok::eof))
4797a614d8380297fcd2bc23986241905d97222948cRichard Smith      ConsumeAnyToken();
4807a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
4817a614d8380297fcd2bc23986241905d97222948cRichard Smith  ConsumeAnyToken();
4824cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
4834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
4844cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ConsumeAndStoreUntil - Consume and store the token at the passed token
48572b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// container until the token 'T' is reached (which gets
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// consumed/stored too, if ConsumeFinalToken).
48714b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis/// If StopAtSemi is true, then we will stop early at a ';' character.
48872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor/// Returns true if token 'T1' or 'T2' was found.
4894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// NOTE: This is a specialized version of Parser::SkipUntil.
49072b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregorbool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
49172b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor                                  CachedTokens &Toks,
49214b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis                                  bool StopAtSemi, bool ConsumeFinalToken) {
4934cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // We always want this function to consume at least one token if the first
4944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // token isn't T and if not at EOF.
4954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  bool isFirstTokenConsumed = true;
4964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  while (1) {
4974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // If we found one of the tokens, stop and return true.
49872b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor    if (Tok.is(T1) || Tok.is(T2)) {
49972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor      if (ConsumeFinalToken) {
50072b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor        Toks.push_back(Tok);
50172b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor        ConsumeAnyToken();
50272b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor      }
5034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      return true;
5044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    }
5054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
5064cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    switch (Tok.getKind()) {
5074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::eof:
5084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Ran out of tokens.
5094cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      return false;
5104cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
5114cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::l_paren:
5124cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Recursively consume properly-nested parens.
5134cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5144cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeParen();
51514b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
5164cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5174cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::l_square:
5184cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Recursively consume properly-nested square brackets.
5194cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5204cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBracket();
52114b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
5224cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5234cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::l_brace:
5244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // Recursively consume properly-nested braces.
5254cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5264cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBrace();
52714b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
5284cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5294cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
5304cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
5314cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // Since the user wasn't looking for this token (if they were, it would
5324cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // already be handled), this isn't balanced.  If there is a LHS token at a
5334cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // higher level, we will assume that this matches the unbalanced token
5344cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
5354cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::r_paren:
5364cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      if (ParenCount && !isFirstTokenConsumed)
5374cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis        return false;  // Matches something.
5384cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5394cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeParen();
5404cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5414cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::r_square:
5424cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      if (BracketCount && !isFirstTokenConsumed)
5434cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis        return false;  // Matches something.
5444cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5454cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBracket();
5464cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5474cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::r_brace:
5484cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      if (BraceCount && !isFirstTokenConsumed)
5494cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis        return false;  // Matches something.
5504cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5514cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeBrace();
5524cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5534cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
5544cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::string_literal:
5554cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    case tok::wide_string_literal:
5564cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5574cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeStringToken();
5584cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
55914b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis    case tok::semi:
56014b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      if (StopAtSemi)
56114b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis        return false;
56214b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis      // FALL THROUGH.
5634cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    default:
5644cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      // consume this token.
5654cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      Toks.push_back(Tok);
5664cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      ConsumeToken();
5674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
5684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    }
5694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    isFirstTokenConsumed = false;
5704cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
5714cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
572