ParseDeclCXX.cpp revision 0e2c34f92f00628d48968dfea096d36381f494cb
18f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
28f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//
38f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//                     The LLVM Compiler Infrastructure
48f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
78f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//
88f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//===----------------------------------------------------------------------===//
98f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//
108f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//  This file implements the C++ Declaration portions of the Parser interfaces.
118f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//
128f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//===----------------------------------------------------------------------===//
138f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner
141b7f89846f10681ce3cbe89ef5d60691d55bd918Douglas Gregor#include "clang/Parse/Parser.h"
1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "RAIIObjectsForParser.h"
16651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/ASTContext.h"
17651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/DeclTemplate.h"
18651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Basic/Attributes.h"
193f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose#include "clang/Basic/CharInfo.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/OperatorKinds.h"
210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include "clang/Basic/TargetInfo.h"
22500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
2319510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
2419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
25f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
2655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h"
27e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall#include "clang/Sema/SemaDiagnostic.h"
288fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer#include "llvm/ADT/SmallString.h"
298f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattnerusing namespace clang;
308f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner
318f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// ParseNamespace - We know that the current token is a namespace keyword. This
32d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// may either be a top level namespace or a block-level namespace alias. If
33d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// there was an inline keyword, it has already been parsed.
348f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///
358f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///       namespace-definition: [C++ 7.3: basic.namespace]
368f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///         named-namespace-definition
378f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///         unnamed-namespace-definition
388f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///
398f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///       unnamed-namespace-definition:
40d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl///         'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
418f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///
428f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///       named-namespace-definition:
438f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///         original-namespace-definition
448f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///         extension-namespace-definition
458f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///
468f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///       original-namespace-definition:
47d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl///         'inline'[opt] 'namespace' identifier attributes[opt]
48d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl///             '{' namespace-body '}'
498f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///
508f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///       extension-namespace-definition:
51d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl///         'inline'[opt] 'namespace' original-namespace-name
52d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl///             '{' namespace-body '}'
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
548f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///       namespace-alias-definition:  [C++ 7.3.2: namespace.alias]
558f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///         'namespace' identifier '=' qualified-namespace-specifier ';'
568f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner///
57d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseNamespace(unsigned Context,
58d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl                             SourceLocation &DeclEnd,
59d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl                             SourceLocation InlineLoc) {
6004d6666eee19bbf25bdfcb8e79eb8b00ace03f0cChris Lattner  assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
618f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  SourceLocation NamespaceLoc = ConsumeToken();  // eat the 'namespace'.
629735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
63a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
6449f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  if (Tok.is(tok::code_completion)) {
6523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteNamespaceDecl(getCurScope());
667d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
6849f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  }
69193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
708f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  SourceLocation IdentLoc;
716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *Ident = nullptr;
72f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  std::vector<SourceLocation> ExtraIdentLoc;
73f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  std::vector<IdentifierInfo*> ExtraIdent;
74f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  std::vector<SourceLocation> ExtraNamespaceLoc;
756a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor
76176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ParsedAttributesWithRange attrs(AttrFactory);
77176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  SourceLocation attrLoc;
78176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
79176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (!getLangOpts().CPlusPlus1z)
80176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
81176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          << 0 /*namespace*/;
82176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    attrLoc = Tok.getLocation();
83176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ParseCXX11Attributes(attrs);
84176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8604d6666eee19bbf25bdfcb8e79eb8b00ace03f0cChris Lattner  if (Tok.is(tok::identifier)) {
878f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner    Ident = Tok.getIdentifierInfo();
888f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner    IdentLoc = ConsumeToken();  // eat the identifier.
89f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
90f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      ExtraNamespaceLoc.push_back(ConsumeToken());
91f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      ExtraIdent.push_back(Tok.getIdentifierInfo());
92f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      ExtraIdentLoc.push_back(ConsumeToken());
93f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    }
948f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  }
951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // A nested namespace definition cannot have attributes.
97176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
98176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
99176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
1008f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  // Read label attributes, if present.
1016a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor  if (Tok.is(tok::kw___attribute)) {
102176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    attrLoc = Tok.getLocation();
1037f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ParseGNUAttributes(attrs);
1046a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor  }
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1066a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor  if (Tok.is(tok::equal)) {
1076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!Ident) {
108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(Tok, diag::err_expected) << tok::identifier;
109e1bb329744ec98ca921bfc4f0888cff7ab4d1bf4Nico Weber      // Skip to end of the definition and eat the ';'.
110e1bb329744ec98ca921bfc4f0888cff7ab4d1bf4Nico Weber      SkipUntil(tok::semi);
1116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
112e1bb329744ec98ca921bfc4f0888cff7ab4d1bf4Nico Weber    }
113176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (attrLoc.isValid())
114176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
115d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    if (InlineLoc.isValid())
116d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      Diag(InlineLoc, diag::err_inline_namespace_alias)
117d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl          << FixItHint::CreateRemoval(InlineLoc);
1189735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian    return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
1196a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor  }
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
1224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
1234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Ident)
125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(Tok, diag::err_expected) << tok::l_brace;
126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    else
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
1286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1295144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner  }
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
13223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
13323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      getCurScope()->getFnParent()) {
1344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
1358fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace);
1366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
13795f1b15ce1b281c8517d77792abe540753fbcc12Douglas Gregor  }
13895f1b15ce1b281c8517d77792abe540753fbcc12Douglas Gregor
139176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (ExtraIdent.empty()) {
140176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Normal namespace definition, not a nested-namespace-definition.
141176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  } else if (InlineLoc.isValid()) {
142176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
143176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  } else if (getLangOpts().CPlusPlus1z) {
144176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(ExtraNamespaceLoc[0],
145176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines         diag::warn_cxx14_compat_nested_namespace_definition);
146176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  } else {
147f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    TentativeParsingAction TPA(*this);
1488fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace, StopBeforeMatch);
149f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    Token rBraceToken = Tok;
150f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    TPA.Revert();
151f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
152f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    if (!rBraceToken.is(tok::r_brace)) {
153176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
154f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu          << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
155f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    } else {
1569910df05e9b5f03043f4d8dc12ea1bbb722664dfBenjamin Kramer      std::string NamespaceFix;
157f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
158f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu           E = ExtraIdent.end(); I != E; ++I) {
159f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu        NamespaceFix += " { namespace ";
160f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu        NamespaceFix += (*I)->getName();
161f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      }
1629910df05e9b5f03043f4d8dc12ea1bbb722664dfBenjamin Kramer
163f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      std::string RBraces;
1649910df05e9b5f03043f4d8dc12ea1bbb722664dfBenjamin Kramer      for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
165f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu        RBraces +=  "} ";
1669910df05e9b5f03043f4d8dc12ea1bbb722664dfBenjamin Kramer
167176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
168f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu          << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
169f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu                                                      ExtraIdentLoc.back()),
170f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu                                          NamespaceFix)
171f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu          << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
172f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    }
173f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  }
174f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
17588e64ca96d6c00c6f3bd43772cd325bede795d2aSebastian Redl  // If we're still good, complain about inline namespaces in non-C++0x now.
1767fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith  if (InlineLoc.isValid())
17780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
1787fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
17988e64ca96d6c00c6f3bd43772cd325bede795d2aSebastian Redl
1805144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner  // Enter a scope for the namespace.
1815144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner  ParseScope NamespaceScope(this, Scope::DeclScope);
1822d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis
183d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *NamespcDecl =
184acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara    Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
1854a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                   IdentLoc, Ident, T.getOpenLocation(),
1864a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                   attrs.getList());
1872d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis
188f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
189f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing namespace");
1901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  // Parse the contents of the namespace.  This includes parsing recovery on
192f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  // any improperly nested namespaces.
193f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
1944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                      InlineLoc, attrs, T);
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1965144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner  // Leave the namespace scope.
1975144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner  NamespaceScope.Exit();
1988ba5d792032f475eb653ca6340eb51068df0fa90Argyrios Kyrtzidis
1994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  DeclEnd = T.getCloseLocation();
2004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
2012d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis
2025144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner  return NamespcDecl;
2038f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner}
204c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
205f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu/// ParseInnerNamespace - Parse the contents of a namespace.
206176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesvoid Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
207176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                 std::vector<IdentifierInfo *> &Ident,
208176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                 std::vector<SourceLocation> &NamespaceLoc,
209176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                 unsigned int index, SourceLocation &InlineLoc,
210176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                 ParsedAttributes &attrs,
2114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                 BalancedDelimiterTracker &Tracker) {
212f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  if (index == Ident.size()) {
213651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
214f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      ParsedAttributesWithRange attrs(AttrFactory);
2154e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith      MaybeParseCXX11Attributes(attrs);
216f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      MaybeParseMicrosoftAttributes(attrs);
217f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu      ParseExternalDeclaration(attrs);
218f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    }
2194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    // The caller is what called check -- we are simply calling
2214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    // the close for it.
2224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
223f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
224f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    return;
225f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  }
226f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
227176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // Handle a nested namespace definition.
228176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // FIXME: Preserve the source information through to the AST rather than
229176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // desugaring it here.
230f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  ParseScope NamespaceScope(this, Scope::DeclScope);
231f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  Decl *NamespcDecl =
232f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu    Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
233f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu                                   NamespaceLoc[index], IdentLoc[index],
2344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                   Ident[index], Tracker.getOpenLocation(),
2354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                   attrs.getList());
236f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
237f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
2384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                      attrs, Tracker);
239f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
240f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu  NamespaceScope.Exit();
241f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
2424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
243f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu}
244f858bd817e8d6eac58ae496fa96a2f508fbb286fRichard Trieu
245f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
246f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson/// alias definition.
247f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson///
248d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
2490b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                  SourceLocation AliasLoc,
2500b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                  IdentifierInfo *Alias,
2510b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                  SourceLocation &DeclEnd) {
252f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  assert(Tok.is(tok::equal) && "Not equal token");
2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  ConsumeToken(); // eat the '='.
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25649f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  if (Tok.is(tok::code_completion)) {
25723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
2587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
2596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
26049f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  }
261193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
262f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  CXXScopeSpec SS;
263f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  // Parse (optional) nested-name-specifier.
264efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
265f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson
266f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
267f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson    Diag(Tok, diag::err_expected_namespace_name);
268f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson    // Skip to end of the definition and eat the ';'.
269f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson    SkipUntil(tok::semi);
2706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
271f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  }
272f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson
273f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  // Parse identifier.
27403bd5a1e9a54b62b10ae8aeb6eb5245e2031d98bAnders Carlsson  IdentifierInfo *Ident = Tok.getIdentifierInfo();
27503bd5a1e9a54b62b10ae8aeb6eb5245e2031d98bAnders Carlsson  SourceLocation IdentLoc = ConsumeToken();
2761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
277f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson  // Eat the ';'.
27897144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner  DeclEnd = Tok.getLocation();
279651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
280651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SkipUntil(tok::semi);
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
28303bd5a1e9a54b62b10ae8aeb6eb5245e2031d98bAnders Carlsson                                        SS, IdentLoc, Ident);
284f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson}
285f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson
286c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// ParseLinkage - We know that the current token is a string_literal
287c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// and just before that, that extern was seen.
288c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner///
289c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner///       linkage-specification: [C++ 7.5p2: dcl.link]
290c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner///         'extern' string-literal '{' declaration-seq[opt] '}'
291c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner///         'extern' string-literal declaration
292c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner///
2937d64271b162eaf5cae264ff64465b28af623dc17Chris LattnerDecl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  assert(isTokenStringLiteral() && "Not a string literal!");
295651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ExprResult Lang = ParseStringLiteralExpression(false);
296c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
297074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  ParseScope LinkageScope(this, Scope::DeclScope);
298651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Decl *LinkageSpec =
299651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Lang.isInvalid()
3006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          ? nullptr
301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          : Actions.ActOnStartLinkageSpecification(
302c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
304074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
3050b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
3064e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
3077f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
308193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
309074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (Tok.isNot(tok::l_brace)) {
310f41e33c29cd4b21065956129d5e923f3d73d97d3Abramo Bagnara    // Reset the source range in DS, as the leading "extern"
311f41e33c29cd4b21065956129d5e923f3d73d97d3Abramo Bagnara    // does not really belong to the inner declaration ...
312f41e33c29cd4b21065956129d5e923f3d73d97d3Abramo Bagnara    DS.SetRangeStart(SourceLocation());
313f41e33c29cd4b21065956129d5e923f3d73d97d3Abramo Bagnara    DS.SetRangeEnd(SourceLocation());
314f41e33c29cd4b21065956129d5e923f3d73d97d3Abramo Bagnara    // ... but anyway remember that such an "extern" was seen.
31535f9a196ef897b9559de25aaecd957208f0b4f59Abramo Bagnara    DS.setExternInLinkageSpec(true);
3167f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ParseExternalDeclaration(attrs, &DS);
317651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
318651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             getCurScope(), LinkageSpec, SourceLocation())
3196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                       : nullptr;
3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
321f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor
32263a011378d4b9483ce24400c163cb8d65ea096a5Douglas Gregor  DS.abort();
32363a011378d4b9483ce24400c163cb8d65ea096a5Douglas Gregor
3247f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  ProhibitAttributes(attrs);
325bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
3264a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
3274a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
328651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
329651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned NestedModules = 0;
330651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  while (true) {
331651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    switch (Tok.getKind()) {
332651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_begin:
333651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ++NestedModules;
334651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ParseTopLevelDecl();
335651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      continue;
336651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
337651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_end:
338651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (!NestedModules)
339651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
340651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      --NestedModules;
341651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ParseTopLevelDecl();
342651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      continue;
343651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
344651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::annot_module_include:
345651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ParseTopLevelDecl();
346651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      continue;
347651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
348651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::eof:
349651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      break;
350651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
351651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::r_brace:
352651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (!NestedModules)
353651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
354651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // Fall through.
355651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    default:
356651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ParsedAttributesWithRange attrs(AttrFactory);
357651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      MaybeParseCXX11Attributes(attrs);
358651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      MaybeParseMicrosoftAttributes(attrs);
359651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ParseExternalDeclaration(attrs);
360651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      continue;
361651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
362651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
363651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    break;
364f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor  }
365c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
3664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
367651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
368651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           getCurScope(), LinkageSpec, T.getCloseLocation())
3696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                     : nullptr;
370c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner}
371e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
372f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
373f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// using-directive. Assumes that current token is 'using'.
374d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
37578b810559d89e996e00684335407443936ce34a1John McCall                                         const ParsedTemplateInfo &TemplateInfo,
37678b810559d89e996e00684335407443936ce34a1John McCall                                               SourceLocation &DeclEnd,
377c89edf5aaa08683f4afcf61a7a1d183c08b76498Richard Smith                                             ParsedAttributesWithRange &attrs,
378c89edf5aaa08683f4afcf61a7a1d183c08b76498Richard Smith                                               Decl **OwnedType) {
379f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  assert(Tok.is(tok::kw_using) && "Not using token");
3809735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
3819735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
382f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  // Eat 'using'.
383f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  SourceLocation UsingLoc = ConsumeToken();
384f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
38549f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  if (Tok.is(tok::code_completion)) {
38623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteUsing(getCurScope());
3877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
3886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
38949f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  }
390193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
39178b810559d89e996e00684335407443936ce34a1John McCall  // 'using namespace' means this is a using-directive.
39278b810559d89e996e00684335407443936ce34a1John McCall  if (Tok.is(tok::kw_namespace)) {
39378b810559d89e996e00684335407443936ce34a1John McCall    // Template parameters are always an error here.
39478b810559d89e996e00684335407443936ce34a1John McCall    if (TemplateInfo.Kind) {
39578b810559d89e996e00684335407443936ce34a1John McCall      SourceRange R = TemplateInfo.getSourceRange();
39678b810559d89e996e00684335407443936ce34a1John McCall      Diag(UsingLoc, diag::err_templated_using_directive)
39778b810559d89e996e00684335407443936ce34a1John McCall        << R << FixItHint::CreateRemoval(R);
39878b810559d89e996e00684335407443936ce34a1John McCall    }
39978b810559d89e996e00684335407443936ce34a1John McCall
4009735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian    return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
40178b810559d89e996e00684335407443936ce34a1John McCall  }
402bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
403162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Otherwise, it must be a using-declaration or an alias-declaration.
40478b810559d89e996e00684335407443936ce34a1John McCall
40578b810559d89e996e00684335407443936ce34a1John McCall  // Using declarations can't have attributes.
4067f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  ProhibitAttributes(attrs);
4072f27477a29b6f5365ee545c1cac666cc8b95f518Chris Lattner
4089735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
409a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                    AS_none, OwnedType);
410f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor}
411f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
412f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// ParseUsingDirective - Parse C++ using-directive, assumes
413f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// that current token is 'namespace' and 'using' was already parsed.
414f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///
415f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///       using-directive: [C++ 7.3.p4: namespace.udir]
416f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///        'using' 'namespace' ::[opt] nested-name-specifier[opt]
417f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///                 namespace-name ;
418f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// [GNU] using-directive:
419f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///        'using' 'namespace' ::[opt] nested-name-specifier[opt]
420f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///                 namespace-name attributes[opt] ;
421f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///
422d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseUsingDirective(unsigned Context,
42378b810559d89e996e00684335407443936ce34a1John McCall                                  SourceLocation UsingLoc,
42478b810559d89e996e00684335407443936ce34a1John McCall                                  SourceLocation &DeclEnd,
4257f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                  ParsedAttributes &attrs) {
426f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
427f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
428f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  // Eat 'namespace'.
429f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  SourceLocation NamespcLoc = ConsumeToken();
430f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
43149f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  if (Tok.is(tok::code_completion)) {
43223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteUsingDirective(getCurScope());
4337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
4346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
43549f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor  }
436193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
437f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  CXXScopeSpec SS;
438f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  // Parse (optional) nested-name-specifier.
439efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
440f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
4416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *NamespcName = nullptr;
442f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  SourceLocation IdentLoc = SourceLocation();
443f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
444f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  // Parse namespace-name.
445823c44e6d73141f642e207980b4021ddcf09897bChris Lattner  if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
446f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor    Diag(Tok, diag::err_expected_namespace_name);
447f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor    // If there was invalid namespace name, skip to end of decl, and eat ';'.
448f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor    SkipUntil(tok::semi);
449f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor    // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
4506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
451f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  }
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
453823c44e6d73141f642e207980b4021ddcf09897bChris Lattner  // Parse identifier.
454823c44e6d73141f642e207980b4021ddcf09897bChris Lattner  NamespcName = Tok.getIdentifierInfo();
455823c44e6d73141f642e207980b4021ddcf09897bChris Lattner  IdentLoc = ConsumeToken();
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
457823c44e6d73141f642e207980b4021ddcf09897bChris Lattner  // Parse (optional) attributes (most likely GNU strong-using extension).
458bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  bool GNUAttr = false;
459bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  if (Tok.is(tok::kw___attribute)) {
460bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    GNUAttr = true;
4617f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ParseGNUAttributes(attrs);
462bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  }
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
464823c44e6d73141f642e207980b4021ddcf09897bChris Lattner  // Eat ';'.
46597144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner  DeclEnd = Tok.getLocation();
466651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ExpectAndConsume(tok::semi,
467651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       GNUAttr ? diag::err_expected_semi_after_attribute_list
468651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                               : diag::err_expected_semi_after_namespace_name))
469651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SkipUntil(tok::semi);
470f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
47123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
4727f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     IdentLoc, NamespcName, attrs.getList());
473f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor}
474f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
475162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
476162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// Assumes that 'using' was already seen.
477f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///
478f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///     using-declaration: [C++ 7.3.p3: namespace.udecl]
479f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///       'using' 'typename'[opt] ::[opt] nested-name-specifier
4809cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor///               unqualified-id
4819cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor///       'using' :: unqualified-id
482f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor///
483d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith///     alias-declaration: C++11 [dcl.dcl]p1
484d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith///       'using' identifier attribute-specifier-seq[opt] = type-id ;
485162e1c1b487352434552147967c3dd296ebee2f7Richard Smith///
486d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseUsingDeclaration(unsigned Context,
48778b810559d89e996e00684335407443936ce34a1John McCall                                    const ParsedTemplateInfo &TemplateInfo,
48878b810559d89e996e00684335407443936ce34a1John McCall                                    SourceLocation UsingLoc,
48978b810559d89e996e00684335407443936ce34a1John McCall                                    SourceLocation &DeclEnd,
490c89edf5aaa08683f4afcf61a7a1d183c08b76498Richard Smith                                    AccessSpecifier AS,
491c89edf5aaa08683f4afcf61a7a1d183c08b76498Richard Smith                                    Decl **OwnedType) {
4929cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  CXXScopeSpec SS;
4937ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SourceLocation TypenameLoc;
4948d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella  bool HasTypenameKeyword = false;
4952edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
4965eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  // Check for misplaced attributes before the identifier in an
4975eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  // alias-declaration.
4985eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
4995eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  MaybeParseCXX11Attributes(MisplacedAttrs);
5009cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
5019cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  // Ignore optional 'typename'.
50212c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor  // FIXME: This is wrong; we should parse this as a typename-specifier.
503651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TryConsumeToken(tok::kw_typename, TypenameLoc))
5048d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    HasTypenameKeyword = true;
5059cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
506176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (Tok.is(tok::kw___super)) {
507176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
508176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SkipUntil(tok::semi);
509176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return nullptr;
510176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
511176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
5129cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  // Parse nested-name-specifier.
5136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *LastII = nullptr;
5142db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
5156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 /*MayBePseudoDtor=*/nullptr,
5166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 /*IsTypename=*/false,
5172db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                 /*LastII=*/&LastII);
5189cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
5199cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  // Check nested-name specifier.
5209cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  if (SS.isInvalid()) {
5219cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    SkipUntil(tok::semi);
5226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
5239cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  }
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5252db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  SourceLocation TemplateKWLoc;
5262db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  UnqualifiedId Name;
5272db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
528193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam  // Parse the unqualified-id. We allow parsing of both constructor and
52912c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor  // destructor names and allow the action module to diagnose any semantic
53012c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor  // errors.
5312db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  //
5322db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  // C++11 [class.qual]p2:
5332db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  //   [...] in a using-declaration that is a member-declaration, if the name
5342db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  //   specified after the nested-name-specifier is the same as the identifier
5352db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  //   or the simple-template-id's template-name in the last component of the
5362db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  //   nested-name-specifier, the name is [...] considered to name the
5372db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  //   constructor.
5382db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
5392db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
5402db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
5412db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      !SS.getScopeRep()->getAsNamespace() &&
5422db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      !SS.getScopeRep()->getAsNamespaceAlias()) {
5432db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    SourceLocation IdLoc = ConsumeToken();
5442db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
5452db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    Name.setConstructorName(Type, IdLoc, IdLoc);
5462db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
5472db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                /*AllowDestructorName=*/ true,
5482db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                /*AllowConstructorName=*/ true, ParsedType(),
5492db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                TemplateKWLoc, Name)) {
5509cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    SkipUntil(tok::semi);
5516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
5529cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  }
553193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
5545eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  ParsedAttributesWithRange Attrs(AttrFactory);
555df1cce5bcd367ee47f4a7579c3a1cb4618248514Richard Smith  MaybeParseGNUAttributes(Attrs);
5566b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  MaybeParseCXX11Attributes(Attrs);
557162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
558162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Maybe this is an alias-declaration.
559162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypeResult TypeAlias;
5605eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  bool IsAliasDecl = Tok.is(tok::equal);
561162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsAliasDecl) {
5625eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    // If we had any misplaced attributes from earlier, this is where they
5635eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    // should have been written.
5645eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    if (MisplacedAttrs.Range.isValid()) {
5655eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
5665eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith        << FixItHint::CreateInsertionFromRange(
5675eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith               Tok.getLocation(),
5685eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith               CharSourceRange::getTokenRange(MisplacedAttrs.Range))
5695eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith        << FixItHint::CreateRemoval(MisplacedAttrs.Range);
5705eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      Attrs.takeAllFrom(MisplacedAttrs);
5715eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    }
5725eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith
573162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    ConsumeToken();
574162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
57580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
5767fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::warn_cxx98_compat_alias_declaration :
5777fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::ext_alias_declaration);
578162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
5793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // Type alias templates cannot be specialized.
5803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    int SpecKind = -1;
581536e9c1f103f3e59ed47e35090819eb93596c35bRichard Smith    if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
582536e9c1f103f3e59ed47e35090819eb93596c35bRichard Smith        Name.getKind() == UnqualifiedId::IK_TemplateId)
5833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      SpecKind = 0;
5843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
5853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      SpecKind = 1;
5863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
5873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      SpecKind = 2;
5883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (SpecKind != -1) {
5893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      SourceRange Range;
5903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      if (SpecKind == 0)
5913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith        Range = SourceRange(Name.TemplateId->LAngleLoc,
5923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                            Name.TemplateId->RAngleLoc);
5933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      else
5943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith        Range = TemplateInfo.getSourceRange();
5953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
5963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith        << SpecKind << Range;
5973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      SkipUntil(tok::semi);
5986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
5993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
6003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
601162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    // Name must be an identifier.
602162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (Name.getKind() != UnqualifiedId::IK_Identifier) {
603162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
604162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      // No removal fixit: can't recover from this.
605162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      SkipUntil(tok::semi);
6066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
6078d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    } else if (HasTypenameKeyword)
608162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
609162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
610162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                             SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
611162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    else if (SS.isNotEmpty())
612162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
613162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        << FixItHint::CreateRemoval(SS.getRange());
614162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
6156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind ?
6163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                              Declarator::AliasTemplateContext :
6176b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith                              Declarator::AliasDeclContext, AS, OwnedType,
6186b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith                              &Attrs);
6192edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  } else {
6202edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // C++11 attributes are not allowed on a using-declaration, but GNU ones
6212edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // are.
6225eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    ProhibitAttributes(MisplacedAttrs);
6236b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith    ProhibitAttributes(Attrs);
6242edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
625162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    // Parse (optional) attributes (most likely GNU strong-using extension).
6266b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith    MaybeParseGNUAttributes(Attrs);
6272edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6299cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  // Eat ';'.
6309cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  DeclEnd = Tok.getLocation();
631651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ExpectAndConsume(tok::semi, diag::err_expected_after,
632651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       !Attrs.empty() ? "attributes list"
633651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                      : IsAliasDecl ? "alias declaration"
634651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                    : "using declaration"))
635651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SkipUntil(tok::semi);
6369cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
63778b810559d89e996e00684335407443936ce34a1John McCall  // Diagnose an attempt to declare a templated using-declaration.
638d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith  // In C++11, alias-declarations can be templates:
639162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  //   template <...> using id = type;
6403e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (TemplateInfo.Kind && !IsAliasDecl) {
64178b810559d89e996e00684335407443936ce34a1John McCall    SourceRange R = TemplateInfo.getSourceRange();
64278b810559d89e996e00684335407443936ce34a1John McCall    Diag(UsingLoc, diag::err_templated_using_declaration)
64378b810559d89e996e00684335407443936ce34a1John McCall      << R << FixItHint::CreateRemoval(R);
64478b810559d89e996e00684335407443936ce34a1John McCall
64578b810559d89e996e00684335407443936ce34a1John McCall    // Unfortunately, we have to bail out instead of recovering by
64678b810559d89e996e00684335407443936ce34a1John McCall    // ignoring the parameters, just in case the nested name specifier
64778b810559d89e996e00684335407443936ce34a1John McCall    // depends on the parameters.
6486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
64978b810559d89e996e00684335407443936ce34a1John McCall  }
65078b810559d89e996e00684335407443936ce34a1John McCall
651480b53cfff18c40d10fcb09b0185a9b75dfd491eDouglas Gregor  // "typename" keyword is allowed for identifiers only,
652480b53cfff18c40d10fcb09b0185a9b75dfd491eDouglas Gregor  // because it may be a type definition.
6538d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella  if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
654480b53cfff18c40d10fcb09b0185a9b75dfd491eDouglas Gregor    Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
655480b53cfff18c40d10fcb09b0185a9b75dfd491eDouglas Gregor      << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
6568d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    // Proceed parsing, but reset the HasTypenameKeyword flag.
6578d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    HasTypenameKeyword = false;
658480b53cfff18c40d10fcb09b0185a9b75dfd491eDouglas Gregor  }
659480b53cfff18c40d10fcb09b0185a9b75dfd491eDouglas Gregor
6603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (IsAliasDecl) {
6613e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
6625354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    MultiTemplateParamsArg TemplateParamsArg(
6636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TemplateParams ? TemplateParams->data() : nullptr,
6643e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      TemplateParams ? TemplateParams->size() : 0);
6653e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
6666b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith                                         UsingLoc, Name, Attrs.getList(),
6676b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith                                         TypeAlias);
6683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
669162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
6708d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella  return Actions.ActOnUsingDeclaration(getCurScope(), AS,
6718d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       /* HasUsingKeyword */ true, UsingLoc,
6728d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       SS, Name, Attrs.getList(),
6738d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       HasTypenameKeyword, TypenameLoc);
674f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor}
675f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor
676ffbe9b9c64ab2e94b9d48ec56e511f75826fc80aBenjamin Kramer/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
677511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson///
678c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne/// [C++0x] static_assert-declaration:
679c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne///           static_assert ( constant-expression  ,  string-literal  ) ;
680c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne///
681ffbe9b9c64ab2e94b9d48ec56e511f75826fc80aBenjamin Kramer/// [C11]   static_assert-declaration:
682c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne///           _Static_assert ( constant-expression  ,  string-literal  ) ;
683511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson///
684d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
685c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
686c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne         "Not a static_assert declaration");
687c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne
6884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
689ffbe9b9c64ab2e94b9d48ec56e511f75826fc80aBenjamin Kramer    Diag(Tok, diag::ext_c11_static_assert);
690841804baff6ea8ba1904a2ba81265aae1479e882Richard Smith  if (Tok.is(tok::kw_static_assert))
691841804baff6ea8ba1904a2ba81265aae1479e882Richard Smith    Diag(Tok, diag::warn_cxx98_compat_static_assert);
692c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne
693511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  SourceLocation StaticAssertLoc = ConsumeToken();
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
6964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
697651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(Tok, diag::err_expected) << tok::l_paren;
6983686c71ff92e4357a78993a16a27185f16ab6234Richard Smith    SkipMalformedDecl();
6996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
700511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  }
7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
70260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult AssertExpr(ParseConstantExpression());
703511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  if (AssertExpr.isInvalid()) {
7043686c71ff92e4357a78993a16a27185f16ab6234Richard Smith    SkipMalformedDecl();
7056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
706511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  }
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
708c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  ExprResult AssertMessage;
709c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  if (Tok.is(tok::r_paren)) {
710c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    Diag(Tok, getLangOpts().CPlusPlus1z
711176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                  ? diag::warn_cxx14_compat_static_assert_no_message
712c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                  : diag::ext_static_assert_no_message)
713c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      << (getLangOpts().CPlusPlus1z
714c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines              ? FixItHint()
715c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines              : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
716c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  } else {
717c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    if (ExpectAndConsume(tok::comma)) {
718c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      SkipUntil(tok::semi);
719c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      return nullptr;
720c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    }
721ad5f960f9e42568a87bf5e03dce7ad878f9ba6daAnders Carlsson
722c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    if (!isTokenStringLiteral()) {
723c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      Diag(Tok, diag::err_expected_string_literal)
724c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        << /*Source='static_assert'*/1;
725c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      SkipMalformedDecl();
726c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      return nullptr;
727c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    }
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
729c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    AssertMessage = ParseStringLiteralExpression();
730c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    if (AssertMessage.isInvalid()) {
731c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      SkipMalformedDecl();
732c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      return nullptr;
733c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    }
73499831e4677a7e2e051af636221694d60ba31fcdbRichard Smith  }
735511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson
7364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
7371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73897144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner  DeclEnd = Tok.getLocation();
7399ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
740511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson
7419ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
742c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                              AssertExpr.get(),
743c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                              AssertMessage.get(),
7444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                              T.getCloseLocation());
745511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson}
746511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson
747a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
7486fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson///
7496fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson/// 'decltype' ( expression )
750a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith/// 'decltype' ( 'auto' )      [C++1y]
7516fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson///
75242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid BlaikieSourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
75342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
75442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie           && "Not a decltype specifier");
75542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
75642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  ExprResult Result;
75742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  SourceLocation StartLoc = Tok.getLocation();
75842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  SourceLocation EndLoc;
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (Tok.is(tok::annot_decltype)) {
76142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    Result = getExprAnnotation(Tok);
76242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    EndLoc = Tok.getAnnotationEndLoc();
76342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    ConsumeToken();
76442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Result.isInvalid()) {
76542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      DS.SetTypeSpecError();
76642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return EndLoc;
76742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
76842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  } else {
769c7b5543ad32a5c265c02e71b2a6f9856440ed13fRichard Smith    if (Tok.getIdentifierInfo()->isStr("decltype"))
770c7b5543ad32a5c265c02e71b2a6f9856440ed13fRichard Smith      Diag(Tok, diag::warn_cxx98_compat_decltype);
77139304fad1c8a7b7e64121e9ae544b18e460b682cRichard Smith
77242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    ConsumeToken();
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    BalancedDelimiterTracker T(*this, tok::l_paren);
77542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (T.expectAndConsume(diag::err_expected_lparen_after,
77642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                           "decltype", tok::r_paren)) {
77742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      DS.SetTypeSpecError();
77842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return T.getOpenLocation() == Tok.getLocation() ?
77942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie             StartLoc : T.getOpenLocation();
78042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
782a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    // Check for C++1y 'decltype(auto)'.
783a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    if (Tok.is(tok::kw_auto)) {
784a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      // No need to disambiguate here: an expression can't start with 'auto',
785a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      // because the typename-specifier in a function-style cast operation can't
786a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      // be 'auto'.
787a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      Diag(Tok.getLocation(),
788176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           getLangOpts().CPlusPlus14
789a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith             ? diag::warn_cxx11_compat_decltype_auto_type_specifier
790a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith             : diag::ext_decltype_auto_type_specifier);
791a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      ConsumeToken();
792a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    } else {
793a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      // Parse the expression
794a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
795a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      // C++11 [dcl.type.simple]p4:
796a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      //   The operand of the decltype specifier is an unevaluated operand.
797a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
7986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                   nullptr,/*IsDecltype=*/true);
799176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Result = Actions.CorrectDelayedTyposInExpr(ParseExpression());
800a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      if (Result.isInvalid()) {
801a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        DS.SetTypeSpecError();
8028fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
803a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          EndLoc = ConsumeParen();
8041e584697aa795f915cd46afefd4e1141ee356b8cArgyrios Kyrtzidis        } else {
805a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
806a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            // Backtrack to get the location of the last token before the semi.
807a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            PP.RevertCachedTokens(2);
808a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            ConsumeToken(); // the semi.
809a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            EndLoc = ConsumeAnyToken();
810a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            assert(Tok.is(tok::semi));
811a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          } else {
812a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            EndLoc = Tok.getLocation();
813a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          }
8141e584697aa795f915cd46afefd4e1141ee356b8cArgyrios Kyrtzidis        }
815a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        return EndLoc;
8161e584697aa795f915cd46afefd4e1141ee356b8cArgyrios Kyrtzidis      }
817a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
818c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      Result = Actions.ActOnDecltypeExpression(Result.get());
81942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
82042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
82142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    // Match the ')'
82242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    T.consumeClose();
82342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (T.getCloseLocation().isInvalid()) {
82442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      DS.SetTypeSpecError();
82542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      // FIXME: this should return the location of the last token
82642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      //        that was consumed (by "consumeClose()")
82742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return T.getCloseLocation();
82842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
82942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
83076f3f69db1416425070177243e9f390122c553e0Richard Smith    if (Result.isInvalid()) {
83176f3f69db1416425070177243e9f390122c553e0Richard Smith      DS.SetTypeSpecError();
83276f3f69db1416425070177243e9f390122c553e0Richard Smith      return T.getCloseLocation();
83376f3f69db1416425070177243e9f390122c553e0Richard Smith    }
83476f3f69db1416425070177243e9f390122c553e0Richard Smith
83542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    EndLoc = T.getCloseLocation();
83642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
837a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  assert(!Result.isInvalid());
8386fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson
8396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const char *PrevSpec = nullptr;
840fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
841651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
8426fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  // Check for duplicate type specifiers (e.g. "int decltype(a)").
843a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  if (Result.get()
844a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
845c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                             DiagID, Result.get(), Policy)
846a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
847651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             DiagID, Policy)) {
848fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    Diag(StartLoc, DiagID) << PrevSpec;
84942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    DS.SetTypeSpecError();
85042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
85142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  return EndLoc;
85242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie}
85342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
85442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikievoid Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
85542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               SourceLocation StartLoc,
85642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               SourceLocation EndLoc) {
85742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  // make sure we have a token we can turn into an annotation token
85842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (PP.isBacktrackEnabled())
85942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    PP.RevertCachedTokens(1);
86042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  else
86142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    PP.EnterToken(Tok);
86242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
86342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  Tok.setKind(tok::annot_decltype);
864a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  setExprAnnotation(Tok,
865a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                    DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
866a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                    DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
867a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                    ExprError());
86842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  Tok.setAnnotationEndLoc(EndLoc);
86942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  Tok.setLocation(StartLoc);
87042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  PP.AnnotateCachedTokens(Tok);
8716fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson}
8726fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson
873db5d44b775c60166074acd184ca9f1981c10c2a7Sean Huntvoid Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
874db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  assert(Tok.is(tok::kw___underlying_type) &&
875db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt         "Not an underlying type specifier");
876db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
877db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  SourceLocation StartLoc = ConsumeToken();
8784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
8794a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after,
8804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                       "__underlying_type", tok::r_paren)) {
881db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    return;
882db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  }
883db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
884db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  TypeResult Result = ParseTypeName();
885db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  if (Result.isInvalid()) {
8868fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
887db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    return;
888db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  }
889db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
890db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  // Match the ')'
8914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
8924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.getCloseLocation().isInvalid())
893db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    return;
894db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
8956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const char *PrevSpec = nullptr;
896db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  unsigned DiagID;
897ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
898c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                         DiagID, Result.get(),
899651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         Actions.getASTContext().getPrintingPolicy()))
900db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Diag(StartLoc, DiagID) << PrevSpec;
9012d77634e839880704d51656bebd1d2daff661d4eEnea Zaffanella  DS.setTypeofParensRange(T.getRange());
902db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt}
903db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
90409048df0b3f472091b2204e531d6b6019244884bDavid Blaikie/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
90509048df0b3f472091b2204e531d6b6019244884bDavid Blaikie/// class name or decltype-specifier. Note that we only check that the result
90609048df0b3f472091b2204e531d6b6019244884bDavid Blaikie/// names a type; semantic analysis will need to verify that the type names a
90709048df0b3f472091b2204e531d6b6019244884bDavid Blaikie/// class. The result is either a type or null, depending on whether a type
90809048df0b3f472091b2204e531d6b6019244884bDavid Blaikie/// name was found.
90942a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor///
910053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///       base-type-specifier: [C++11 class.derived]
91109048df0b3f472091b2204e531d6b6019244884bDavid Blaikie///         class-or-decltype
912053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///       class-or-decltype: [C++11 class.derived]
91309048df0b3f472091b2204e531d6b6019244884bDavid Blaikie///         nested-name-specifier[opt] class-name
91409048df0b3f472091b2204e531d6b6019244884bDavid Blaikie///         decltype-specifier
915053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///       class-name: [C++ class.name]
91642a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor///         identifier
9177f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor///         simple-template-id
9181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
919053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith/// In C++98, instead of base-type-specifier, we have:
920053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///
921053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///         ::[opt] nested-name-specifier[opt] class-name
922176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesTypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
923176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                          SourceLocation &EndLocation) {
9247fe3878a36750515fb9772414ecb2489cf149d19David Blaikie  // Ignore attempts to use typename
9257fe3878a36750515fb9772414ecb2489cf149d19David Blaikie  if (Tok.is(tok::kw_typename)) {
9267fe3878a36750515fb9772414ecb2489cf149d19David Blaikie    Diag(Tok, diag::err_expected_class_name_not_template)
9277fe3878a36750515fb9772414ecb2489cf149d19David Blaikie      << FixItHint::CreateRemoval(Tok.getLocation());
9287fe3878a36750515fb9772414ecb2489cf149d19David Blaikie    ConsumeToken();
9297fe3878a36750515fb9772414ecb2489cf149d19David Blaikie  }
9307fe3878a36750515fb9772414ecb2489cf149d19David Blaikie
931152aa4b87633754801598ee282e1a17c3ec49257David Blaikie  // Parse optional nested-name-specifier
932152aa4b87633754801598ee282e1a17c3ec49257David Blaikie  CXXScopeSpec SS;
933152aa4b87633754801598ee282e1a17c3ec49257David Blaikie  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
934152aa4b87633754801598ee282e1a17c3ec49257David Blaikie
935152aa4b87633754801598ee282e1a17c3ec49257David Blaikie  BaseLoc = Tok.getLocation();
936152aa4b87633754801598ee282e1a17c3ec49257David Blaikie
93722216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie  // Parse decltype-specifier
93842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  // tok == kw_decltype is just error recovery, it can only happen when SS
93942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  // isn't empty
94042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
941152aa4b87633754801598ee282e1a17c3ec49257David Blaikie    if (SS.isNotEmpty())
942152aa4b87633754801598ee282e1a17c3ec49257David Blaikie      Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
943152aa4b87633754801598ee282e1a17c3ec49257David Blaikie        << FixItHint::CreateRemoval(SS.getRange());
94422216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie    // Fake up a Declarator to use with ActOnTypeName.
94522216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie    DeclSpec DS(AttrFactory);
94622216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie
947b57775709666e50cd925f9fc589d0fd895fc79a6David Blaikie    EndLocation = ParseDecltypeSpecifier(DS);
94822216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie
94922216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie    Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
95022216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie    return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
95122216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie  }
95222216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie
9537f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor  // Check whether we have a template-id that names a type.
9547f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
95525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
956d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor    if (TemplateId->Kind == TNK_Type_template ||
957d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor        TemplateId->Kind == TNK_Dependent_template_name) {
958059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
9597f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor
9607f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor      assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
961b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ParsedType Type = getTypeAnnotation(Tok);
9627f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor      EndLocation = Tok.getAnnotationEndLoc();
9637f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor      ConsumeToken();
96431a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor
96531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor      if (Type)
96631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        return Type;
96731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor      return true;
9687f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor    }
9697f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor
9707f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor    // Fall through to produce an error below.
9717f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor  }
9727f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor
97342a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor  if (Tok.isNot(tok::identifier)) {
9741ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_class_name);
97531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor    return true;
97642a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor  }
97742a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor
97884d0a19828599e8623223632d59447fd498999cfDouglas Gregor  IdentifierInfo *Id = Tok.getIdentifierInfo();
97984d0a19828599e8623223632d59447fd498999cfDouglas Gregor  SourceLocation IdLoc = ConsumeToken();
98084d0a19828599e8623223632d59447fd498999cfDouglas Gregor
98184d0a19828599e8623223632d59447fd498999cfDouglas Gregor  if (Tok.is(tok::less)) {
98284d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // It looks the user intended to write a template-id here, but the
98384d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // template-name was wrong. Try to fix that.
98484d0a19828599e8623223632d59447fd498999cfDouglas Gregor    TemplateNameKind TNK = TNK_Type_template;
98584d0a19828599e8623223632d59447fd498999cfDouglas Gregor    TemplateTy Template;
98623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
987059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                             &SS, Template, TNK)) {
98884d0a19828599e8623223632d59447fd498999cfDouglas Gregor      Diag(IdLoc, diag::err_unknown_template_name)
98984d0a19828599e8623223632d59447fd498999cfDouglas Gregor        << Id;
99084d0a19828599e8623223632d59447fd498999cfDouglas Gregor    }
991193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
99262f675cf69ca52c163fd9c0564d84356bb7ffca1Serge Pavlov    if (!Template) {
99362f675cf69ca52c163fd9c0564d84356bb7ffca1Serge Pavlov      TemplateArgList TemplateArgs;
99462f675cf69ca52c163fd9c0564d84356bb7ffca1Serge Pavlov      SourceLocation LAngleLoc, RAngleLoc;
99562f675cf69ca52c163fd9c0564d84356bb7ffca1Serge Pavlov      ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
99662f675cf69ca52c163fd9c0564d84356bb7ffca1Serge Pavlov          true, LAngleLoc, TemplateArgs, RAngleLoc);
99784d0a19828599e8623223632d59447fd498999cfDouglas Gregor      return true;
99862f675cf69ca52c163fd9c0564d84356bb7ffca1Serge Pavlov    }
99984d0a19828599e8623223632d59447fd498999cfDouglas Gregor
1000193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam    // Form the template name
100184d0a19828599e8623223632d59447fd498999cfDouglas Gregor    UnqualifiedId TemplateName;
100284d0a19828599e8623223632d59447fd498999cfDouglas Gregor    TemplateName.setIdentifier(Id, IdLoc);
1003193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
100484d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // Parse the full template-id, then turn it into a type.
1005e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1006e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                TemplateName, true))
100784d0a19828599e8623223632d59447fd498999cfDouglas Gregor      return true;
100884d0a19828599e8623223632d59447fd498999cfDouglas Gregor    if (TNK == TNK_Dependent_template_name)
1009059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
1010193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
101184d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // If we didn't end up with a typename token, there's nothing more we
101284d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // can do.
101384d0a19828599e8623223632d59447fd498999cfDouglas Gregor    if (Tok.isNot(tok::annot_typename))
101484d0a19828599e8623223632d59447fd498999cfDouglas Gregor      return true;
1015193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
101684d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // Retrieve the type from the annotation token, consume that token, and
101784d0a19828599e8623223632d59447fd498999cfDouglas Gregor    // return.
101884d0a19828599e8623223632d59447fd498999cfDouglas Gregor    EndLocation = Tok.getAnnotationEndLoc();
1019b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Type = getTypeAnnotation(Tok);
102084d0a19828599e8623223632d59447fd498999cfDouglas Gregor    ConsumeToken();
102184d0a19828599e8623223632d59447fd498999cfDouglas Gregor    return Type;
102284d0a19828599e8623223632d59447fd498999cfDouglas Gregor  }
102384d0a19828599e8623223632d59447fd498999cfDouglas Gregor
102442a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor  // We have an identifier; check whether it is actually a type.
10256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *CorrectedII = nullptr;
1026059101f922de6eb765601459925f4c8914420b23Douglas Gregor  ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
10279e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                        false, ParsedType(),
1028fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                        /*IsCtorOrDtorName=*/false,
1029c1fb54265614845ee1e09856af6e46961c6209f4Kaelyn Uhrain                                        /*NonTrivialTypeSourceInfo=*/true,
1030c1fb54265614845ee1e09856af6e46961c6209f4Kaelyn Uhrain                                        &CorrectedII);
1031193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam  if (!Type) {
1032124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    Diag(IdLoc, diag::err_expected_class_name);
103331a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor    return true;
103442a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor  }
103542a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor
103642a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor  // Consume the identifier.
103784d0a19828599e8623223632d59447fd498999cfDouglas Gregor  EndLocation = IdLoc;
10385606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky
10395606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // Fake up a Declarator to use with ActOnTypeName.
10400b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
10415606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  DS.SetRangeStart(IdLoc);
10425606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  DS.SetRangeEnd(EndLocation);
1043059101f922de6eb765601459925f4c8914420b23Douglas Gregor  DS.getTypeSpecScope() = SS;
10445606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky
10456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const char *PrevSpec = nullptr;
10465606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  unsigned DiagID;
1047651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1048651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     Actions.getASTContext().getPrintingPolicy());
10495606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky
10505606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
10515606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
105242a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor}
105342a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor
1054c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCallvoid Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1055c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall  while (Tok.is(tok::kw___single_inheritance) ||
1056c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall         Tok.is(tok::kw___multiple_inheritance) ||
1057c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall         Tok.is(tok::kw___virtual_inheritance)) {
1058c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall    IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1059c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall    SourceLocation AttrNameLoc = ConsumeToken();
10606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1061651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                 AttributeList::AS_Keyword);
1062c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall  }
1063c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall}
1064c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall
1065c9f351700721150a985f21844fbfec55b04e861dRichard Smith/// Determine whether the following tokens are valid after a type-specifier
1066c9f351700721150a985f21844fbfec55b04e861dRichard Smith/// which could be a standalone declaration. This will conservatively return
1067c9f351700721150a985f21844fbfec55b04e861dRichard Smith/// true if there's any doubt, and is appropriate for insert-';' fixits.
1068139be7007eba3bd491ca50297888be507753a95dRichard Smithbool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
1069c9f351700721150a985f21844fbfec55b04e861dRichard Smith  // This switch enumerates the valid "follow" set for type-specifiers.
1070c9f351700721150a985f21844fbfec55b04e861dRichard Smith  switch (Tok.getKind()) {
1071c9f351700721150a985f21844fbfec55b04e861dRichard Smith  default: break;
1072c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::semi:               // struct foo {...} ;
1073c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::star:               // struct foo {...} *         P;
1074c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::amp:                // struct foo {...} &         R = ...
1075ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  case tok::ampamp:             // struct foo {...} &&        R = ...
1076c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::identifier:         // struct foo {...} V         ;
1077c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::r_paren:            //(struct foo {...} )         {4}
1078c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::annot_cxxscope:     // struct foo {...} a::       b;
1079c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::annot_typename:     // struct foo {...} a         ::b;
1080c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::annot_template_id:  // struct foo {...} a<int>    ::b;
1081c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::l_paren:            // struct foo {...} (         x);
1082c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::comma:              // __builtin_offsetof(struct foo{...} ,
1083ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  case tok::kw_operator:        // struct foo       operator  ++() {...}
1084651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case tok::kw___declspec:      // struct foo {...} __declspec(...)
1085176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case tok::l_square:           // void f(struct f  [         3])
1086176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case tok::ellipsis:           // void f(struct f  ...       [Ns])
1087176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // FIXME: we should emit semantic diagnostic when declaration
1088176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // attribute is in type attribute position.
1089176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case tok::kw___attribute:     // struct foo __attribute__((used)) x;
1090c9f351700721150a985f21844fbfec55b04e861dRichard Smith    return true;
1091139be7007eba3bd491ca50297888be507753a95dRichard Smith  case tok::colon:
1092139be7007eba3bd491ca50297888be507753a95dRichard Smith    return CouldBeBitfield;     // enum E { ... }   :         2;
1093c9f351700721150a985f21844fbfec55b04e861dRichard Smith  // Type qualifiers
1094c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_const:           // struct foo {...} const     x;
1095c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_volatile:        // struct foo {...} volatile  x;
1096c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_restrict:        // struct foo {...} restrict  x;
1097176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case tok::kw__Atomic:         // struct foo {...} _Atomic   x;
10980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case tok::kw___unaligned:     // struct foo {...} __unaligned *x;
1099ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  // Function specifiers
1100ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  // Note, no 'explicit'. An explicit function must be either a conversion
1101ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  // operator or a constructor. Either way, it can't have a return type.
1102ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  case tok::kw_inline:          // struct foo       inline    f();
1103ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  case tok::kw_virtual:         // struct foo       virtual   f();
1104ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  case tok::kw_friend:          // struct foo       friend    f();
1105c9f351700721150a985f21844fbfec55b04e861dRichard Smith  // Storage-class specifiers
1106c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_static:          // struct foo {...} static    x;
1107c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_extern:          // struct foo {...} extern    x;
1108c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_typedef:         // struct foo {...} typedef   x;
1109c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_register:        // struct foo {...} register  x;
1110c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_auto:            // struct foo {...} auto      x;
1111c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_mutable:         // struct foo {...} mutable   x;
1112ba65f505b7cc2551571b299d05d767e0a892aaaeRichard Smith  case tok::kw_thread_local:    // struct foo {...} thread_local x;
1113c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::kw_constexpr:       // struct foo {...} constexpr x;
1114c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // As shown above, type qualifiers and storage class specifiers absolutely
1115c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // can occur after class specifiers according to the grammar.  However,
1116c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // almost no one actually writes code like this.  If we see one of these,
1117c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // it is much more likely that someone missed a semi colon and the
1118c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // type/storage class specifier we're seeing is part of the *next*
1119c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // intended declaration, as in:
1120c9f351700721150a985f21844fbfec55b04e861dRichard Smith    //
1121c9f351700721150a985f21844fbfec55b04e861dRichard Smith    //   struct foo { ... }
1122c9f351700721150a985f21844fbfec55b04e861dRichard Smith    //   typedef int X;
1123c9f351700721150a985f21844fbfec55b04e861dRichard Smith    //
1124c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // We'd really like to emit a missing semicolon error instead of emitting
1125c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // an error on the 'int' saying that you can't have two type specifiers in
1126c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // the same declaration of X.  Because of this, we look ahead past this
1127c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // token to see if it's a type specifier.  If so, we know the code is
1128c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // otherwise invalid, so we can produce the expected semi error.
1129c9f351700721150a985f21844fbfec55b04e861dRichard Smith    if (!isKnownToBeTypeSpecifier(NextToken()))
1130c9f351700721150a985f21844fbfec55b04e861dRichard Smith      return true;
1131c9f351700721150a985f21844fbfec55b04e861dRichard Smith    break;
1132c9f351700721150a985f21844fbfec55b04e861dRichard Smith  case tok::r_brace:  // struct bar { struct foo {...} }
1133c9f351700721150a985f21844fbfec55b04e861dRichard Smith    // Missing ';' at end of struct is accepted as an extension in C mode.
1134c9f351700721150a985f21844fbfec55b04e861dRichard Smith    if (!getLangOpts().CPlusPlus)
1135c9f351700721150a985f21844fbfec55b04e861dRichard Smith      return true;
1136c9f351700721150a985f21844fbfec55b04e861dRichard Smith    break;
11378338a9d28c4a9d06b19b1c5df51e70182914e2dfRichard Smith  case tok::greater:
11388338a9d28c4a9d06b19b1c5df51e70182914e2dfRichard Smith    // template<class T = class X>
11398338a9d28c4a9d06b19b1c5df51e70182914e2dfRichard Smith    return getLangOpts().CPlusPlus;
1140c9f351700721150a985f21844fbfec55b04e861dRichard Smith  }
1141c9f351700721150a985f21844fbfec55b04e861dRichard Smith  return false;
1142c9f351700721150a985f21844fbfec55b04e861dRichard Smith}
1143c9f351700721150a985f21844fbfec55b04e861dRichard Smith
1144e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1145e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1146e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// until we reach the start of a definition or see a token that
114769730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith/// cannot start a definition.
1148e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1149e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       class-specifier: [C++ class]
1150e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         class-head '{' member-specification[opt] '}'
1151e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         class-head '{' member-specification[opt] '}' attributes[opt]
1152e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       class-head:
1153e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         class-key identifier[opt] base-clause[opt]
1154e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         class-key nested-name-specifier identifier base-clause[opt]
1155e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         class-key nested-name-specifier[opt] simple-template-id
1156e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///                          base-clause[opt]
1157e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// [GNU]   class-key attributes[opt] identifier[opt] base-clause[opt]
11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [GNU]   class-key attributes[opt] nested-name-specifier
1159e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///                          identifier base-clause[opt]
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [GNU]   class-key attributes[opt] nested-name-specifier[opt]
1161e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///                          simple-template-id base-clause[opt]
1162e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       class-key:
1163e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'class'
1164e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'struct'
1165e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'union'
1166e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1167e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       elaborated-type-specifier: [C++ dcl.type.elab]
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///         class-key ::[opt] nested-name-specifier[opt] identifier
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///         class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///                          simple-template-id
1171e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1172e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///  Note that the C++ class-specifier and elaborated-type-specifier,
1173e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///  together, subsume the C99 struct-or-union-specifier:
1174e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1175e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       struct-or-union-specifier: [C99 6.7.2.1]
1176e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         struct-or-union identifier[opt] '{' struct-contents '}'
1177e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         struct-or-union identifier
1178e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// [GNU]   struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1179e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///                                                         '}' attributes[opt]
1180e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// [GNU]   struct-or-union attributes[opt] identifier
1181e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       struct-or-union:
1182e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'struct'
1183e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'union'
11844c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattnervoid Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
11854c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner                                 SourceLocation StartLoc, DeclSpec &DS,
11864d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor                                 const ParsedTemplateInfo &TemplateInfo,
1187efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                 AccessSpecifier AS,
11882e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han                                 bool EnteringContext, DeclSpecContext DSC,
1189ad017fa7a4df7389d245d02a49b3c79ed70bedb9Bill Wendling                                 ParsedAttributesWithRange &Attributes) {
119017d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  DeclSpec::TST TagType;
119117d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  if (TagTokKind == tok::kw_struct)
119217d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos    TagType = DeclSpec::TST_struct;
119317d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  else if (TagTokKind == tok::kw___interface)
119417d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos    TagType = DeclSpec::TST_interface;
119517d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  else if (TagTokKind == tok::kw_class)
119617d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos    TagType = DeclSpec::TST_class;
119717d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  else {
11984c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner    assert(TagTokKind == tok::kw_union && "Not a class specifier");
11994c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner    TagType = DeclSpec::TST_union;
12004c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner  }
1201e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1202374929f7e88f6c7a96382b3eb4201b721c418372Douglas Gregor  if (Tok.is(tok::code_completion)) {
1203374929f7e88f6c7a96382b3eb4201b721c418372Douglas Gregor    // Code completion for a struct, class, or union name.
120423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteTag(getCurScope(), TagType);
12057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return cutOffParsing();
1206374929f7e88f6c7a96382b3eb4201b721c418372Douglas Gregor  }
1207193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
1208926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  // C++03 [temp.explicit] 14.7.2/8:
1209926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  //   The usual access checking rules do not apply to names used to specify
1210926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  //   explicit instantiations.
1211926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  //
1212926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  // As an extension we do not perform access checking on the names used to
1213926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  // specify explicit specializations either. This is important to allow
1214926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth  // specializing traits classes for private types.
121513489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  //
121613489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  // Note that we don't suppress if this turns out to be an elaborated
121713489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  // type specifier.
121813489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  bool shouldDelayDiagsInTag =
121913489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall    (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
122013489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall     TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
122113489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
1222926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth
12232edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  ParsedAttributesWithRange attrs(AttrFactory);
1224e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // If attributes exist after tag, parse them.
1225df1cce5bcd367ee47f4a7579c3a1cb4618248514Richard Smith  MaybeParseGNUAttributes(attrs);
1226e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1227f59e17ecf06ac60065e2d02058bd6f21f5d216ccSteve Naroff  // If declspecs exist after tag, parse them.
1228b1d397c23e1f8fd8b404d5731d531e7500f7140dJohn McCall  while (Tok.is(tok::kw___declspec))
12297f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ParseMicrosoftDeclSpec(attrs);
1230193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
1231c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall  // Parse inheritance specifiers.
1232c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall  if (Tok.is(tok::kw___single_inheritance) ||
1233c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      Tok.is(tok::kw___multiple_inheritance) ||
1234c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      Tok.is(tok::kw___virtual_inheritance))
1235df1cce5bcd367ee47f4a7579c3a1cb4618248514Richard Smith    ParseMicrosoftInheritanceClassAttributes(attrs);
1236c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall
1237bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  // If C++0x attributes exist here, parse them.
1238bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  // FIXME: Are we consistent with the ordering of parsing of different
1239bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  // styles of attributes?
12404e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
12411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124207fc1ba7553f2f5bf26984091197311decd9028eMichael Han  // Source location used by FIXIT to insert misplaced
124307fc1ba7553f2f5bf26984091197311decd9028eMichael Han  // C++11 attributes
124407fc1ba7553f2f5bf26984091197311decd9028eMichael Han  SourceLocation AttrFixitLoc = Tok.getLocation();
124507fc1ba7553f2f5bf26984091197311decd9028eMichael Han
1246176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (TagType == DeclSpec::TST_struct &&
12470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      Tok.isNot(tok::identifier) &&
12480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      !Tok.isAnnotation() &&
1249176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Tok.getIdentifierInfo() &&
1250176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      (Tok.is(tok::kw___is_abstract) ||
1251176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_arithmetic) ||
1252176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_array) ||
1253176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_base_of) ||
1254176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_class) ||
1255176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_complete_type) ||
1256176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_compound) ||
1257176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_const) ||
1258176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_constructible) ||
1259176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_convertible) ||
1260176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_convertible_to) ||
1261176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_destructible) ||
1262176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_empty) ||
1263176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_enum) ||
1264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_floating_point) ||
1265176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_final) ||
1266176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_function) ||
1267176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_fundamental) ||
1268176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_integral) ||
1269176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_interface_class) ||
1270176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_literal) ||
1271176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_lvalue_expr) ||
1272176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_lvalue_reference) ||
1273176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_member_function_pointer) ||
1274176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_member_object_pointer) ||
1275176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_member_pointer) ||
1276176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_nothrow_assignable) ||
1277176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_nothrow_constructible) ||
1278176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_nothrow_destructible) ||
1279176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_object) ||
1280176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_pod) ||
1281176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_pointer) ||
1282176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_polymorphic) ||
1283176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_reference) ||
1284176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_rvalue_expr) ||
1285176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_rvalue_reference) ||
1286176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_same) ||
1287176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_scalar) ||
1288176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_sealed) ||
1289176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_signed) ||
1290176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_standard_layout) ||
1291176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_trivial) ||
1292176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_trivially_assignable) ||
1293176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_trivially_constructible) ||
1294176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_trivially_copyable) ||
1295176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_union) ||
1296176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_unsigned) ||
1297176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_void) ||
1298176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___is_volatile)))
1299176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1300176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // name of struct templates, but some are keywords in GCC >= 4.3
1301176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // and Clang. Therefore, when we see the token sequence "struct
1302176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // X", make X into a normal identifier rather than a keyword, to
1303176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // allow libstdc++ 4.2 and libc++ to work properly.
1304176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    TryKeywordIdentFallback(true);
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1306eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // Parse the (optional) nested-name-specifier.
1307aa87d33309f505b68c3bfc17486c93e4d224b24fJohn McCall  CXXScopeSpec &SS = DS.getTypeSpecScope();
13084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1309176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // "FOO : BAR" is not a potential typo for "FOO::BAR".  In this context it
1310176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // is a base-specifier-list.
131108d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner    ColonProtectionRAIIObject X(*this);
1312193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
13130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    CXXScopeSpec Spec;
13140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    bool HasValidSpec = true;
13150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (ParseOptionalCXXScopeSpecifier(Spec, ParsedType(), EnteringContext)) {
1316207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall      DS.SetTypeSpecError();
13170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      HasValidSpec = false;
13180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
13190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (Spec.isSet())
13200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
1321651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag(Tok, diag::err_expected) << tok::identifier;
13220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        HasValidSpec = false;
13230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
13240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (HasValidSpec)
13250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      SS = Spec;
132608d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner  }
1327cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
13282cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor  TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
13292cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor
1330cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // Parse the (optional) class name or simple-template-id.
13316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *Name = nullptr;
1332e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  SourceLocation NameLoc;
13336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TemplateIdAnnotation *TemplateId = nullptr;
1334e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  if (Tok.is(tok::identifier)) {
1335e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    Name = Tok.getIdentifierInfo();
1336e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    NameLoc = ConsumeToken();
1337193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
13384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
1339193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam      // The name was supposed to refer to a template, but didn't.
13402cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      // Eat the template argument list and try to continue parsing this as
13412cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      // a class (or template thereof).
13422cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      TemplateArgList TemplateArgs;
13432cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      SourceLocation LAngleLoc, RAngleLoc;
1344059101f922de6eb765601459925f4c8914420b23Douglas Gregor      if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
13452cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor                                           true, LAngleLoc,
1346314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor                                           TemplateArgs, RAngleLoc)) {
13472cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor        // We couldn't parse the template argument list at all, so don't
13482cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor        // try to give any location information for the list.
13492cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor        LAngleLoc = RAngleLoc = SourceLocation();
13502cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      }
1351193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
13522cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      Diag(NameLoc, diag::err_explicit_spec_non_template)
1353651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1354651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
135517d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos
1356193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam      // Strip off the last template parameter list if it was empty, since
1357c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor      // we've removed its template argument list.
1358c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor      if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1359c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor        if (TemplateParams && TemplateParams->size() > 1) {
1360c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor          TemplateParams->pop_back();
1361c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor        } else {
13626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          TemplateParams = nullptr;
1363193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam          const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
1364c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor            = ParsedTemplateInfo::NonTemplate;
1365c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor        }
1366c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor      } else if (TemplateInfo.Kind
1367c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor                                == ParsedTemplateInfo::ExplicitInstantiation) {
1368c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor        // Pretend this is just a forward declaration.
13696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        TemplateParams = nullptr;
1370193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam        const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
13712cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor          = ParsedTemplateInfo::NonTemplate;
1372193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam        const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
1373c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor          = SourceLocation();
1374c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor        const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1375c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor          = SourceLocation();
13762cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor      }
13772cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor    }
137839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
137925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateId = takeTemplateIdAnnotation(Tok);
138039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    NameLoc = ConsumeToken();
1381cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
1382059101f922de6eb765601459925f4c8914420b23Douglas Gregor    if (TemplateId->Kind != TNK_Type_template &&
1383059101f922de6eb765601459925f4c8914420b23Douglas Gregor        TemplateId->Kind != TNK_Dependent_template_name) {
138439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // The template-name in the simple-template-id refers to
138539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // something other than a class template. Give an appropriate
138639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // error message and skip to the ';'.
138739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      SourceRange Range(NameLoc);
138839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      if (SS.isNotEmpty())
138939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        Range.setBegin(SS.getBeginLoc());
139039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
1391651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // FIXME: Name may be null here.
139239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
13936e91f4bd9a05ca2c58ba11d541d8f9dab5514b76Richard Trieu        << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
139539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      DS.SetTypeSpecError();
13968fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopBeforeMatch);
139739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      return;
1398cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
1399e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  }
1400e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
14017796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  // There are four options here.
14027796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //  - If we are in a trailing return type, this is always just a reference,
14037796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //    and we must not try to parse a definition. For instance,
14047796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //      [] () -> struct S { };
14057796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //    does not define a type.
14067796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //  - If we have 'struct foo {...', 'struct foo :...',
14077796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //    'struct foo final :' or 'struct foo final {', then this is a definition.
14087796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //  - If we have 'struct foo;', then this is either a forward declaration
14097796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //    or a friend declaration, which have to be treated differently.
14107796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  //  - Otherwise we have something like 'struct foo xyz', a reference.
14112e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //
14122e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //  We also detect these erroneous cases to provide better diagnostic for
14132e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //  C++11 attributes parsing.
14142e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //  - attributes follow class name:
14152e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //    struct foo [[]] {};
14162e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //  - attributes appear before or after 'final':
14172e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //    struct foo [[]] final [[]] {};
14182e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  //
141969730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  // However, in type-specifier-seq's, things look like declarations but are
142069730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  // just references, e.g.
142169730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  //   new struct s;
1422d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  // or
142369730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  //   &T::operator struct s;
1424651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
14252e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
14262e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  // If there are attributes after class name, parse them.
14274e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(Attributes);
14282e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
1429651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
1430f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Sema::TagUseKind TUK;
14317796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  if (DSC == DSC_trailing)
14327796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    TUK = Sema::TUK_Reference;
14337796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  else if (Tok.is(tok::l_brace) ||
14347796eb5643244f3134834253ce5ea89107ac21c1Richard Smith           (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
14354e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith           (isCXX11FinalKeyword() &&
14366f42669b7c0b81b07a15a0483aa5e897ce57cb45David Blaikie            (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
1437d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor    if (DS.isFriendSpecified()) {
1438d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor      // C++ [class.friend]p2:
1439d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor      //   A class shall not be defined in a friend declaration.
1440bdad7a2e21686296b78dac6190b78d11c996f6d7Richard Smith      Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
1441d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor        << SourceRange(DS.getFriendSpecLoc());
1442d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor
1443d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor      // Skip everything up to the semicolon, so that this looks like a proper
1444d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor      // friend class (or template thereof) declaration.
14458fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopBeforeMatch);
1446f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      TUK = Sema::TUK_Friend;
1447d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor    } else {
1448d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor      // Okay, this is a class definition.
1449f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      TUK = Sema::TUK_Definition;
1450d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor    }
1451150d853343758281f7b0c44e058ea81da45b79beRichard Smith  } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1452150d853343758281f7b0c44e058ea81da45b79beRichard Smith                                       NextToken().is(tok::kw_alignas))) {
14532e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    // We can't tell if this is a definition or reference
14542e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    // until we skipped the 'final' and C++11 attribute specifiers.
14552e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    TentativeParsingAction PA(*this);
14562e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
14572e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    // Skip the 'final' keyword.
14582e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    ConsumeToken();
14592e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
14602e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    // Skip C++11 attribute specifiers.
14612e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    while (true) {
14622e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han      if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
14632e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han        ConsumeBracket();
14648fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        if (!SkipUntil(tok::r_square, StopAtSemi))
14652e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han          break;
1466150d853343758281f7b0c44e058ea81da45b79beRichard Smith      } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
14672e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han        ConsumeToken();
14682e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han        ConsumeParen();
14698fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        if (!SkipUntil(tok::r_paren, StopAtSemi))
14702e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han          break;
14712e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han      } else {
14722e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han        break;
14732e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han      }
14742e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    }
14752e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
14762e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
14772e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han      TUK = Sema::TUK_Definition;
14782e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    else
14792e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han      TUK = Sema::TUK_Reference;
14802e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
14812e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han    PA.Revert();
1482651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else if (!isTypeSpecifier(DSC) &&
1483c9f351700721150a985f21844fbfec55b04e861dRichard Smith             (Tok.is(tok::semi) ||
1484139be7007eba3bd491ca50297888be507753a95dRichard Smith              (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
1485f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
148617d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos    if (Tok.isNot(tok::semi)) {
1487651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
148817d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos      // A semicolon was missing after this declaration. Diagnose and recover.
1489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ExpectAndConsume(tok::semi, diag::err_expected_after,
1490651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       DeclSpec::getSpecifierName(TagType, PPol));
149117d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos      PP.EnterToken(Tok);
149217d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos      Tok.setKind(tok::semi);
149317d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos    }
1494c9f351700721150a985f21844fbfec55b04e861dRichard Smith  } else
1495f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    TUK = Sema::TUK_Reference;
1496e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
14972e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  // Forbid misplaced attributes. In cases of a reference, we pass attributes
14982e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han  // to caller to handle.
149907fc1ba7553f2f5bf26984091197311decd9028eMichael Han  if (TUK != Sema::TUK_Reference) {
150007fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // If this is not a reference, then the only possible
150107fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // valid place for C++11 attributes to appear here
150207fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // is between class-key and class-name. If there are
150307fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // any attributes after class-name, we try a fixit to move
150407fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // them to the right place.
150507fc1ba7553f2f5bf26984091197311decd9028eMichael Han    SourceRange AttrRange = Attributes.Range;
150607fc1ba7553f2f5bf26984091197311decd9028eMichael Han    if (AttrRange.isValid()) {
150707fc1ba7553f2f5bf26984091197311decd9028eMichael Han      Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
150807fc1ba7553f2f5bf26984091197311decd9028eMichael Han        << AttrRange
150907fc1ba7553f2f5bf26984091197311decd9028eMichael Han        << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
151007fc1ba7553f2f5bf26984091197311decd9028eMichael Han                                               CharSourceRange(AttrRange, true))
151107fc1ba7553f2f5bf26984091197311decd9028eMichael Han        << FixItHint::CreateRemoval(AttrRange);
151207fc1ba7553f2f5bf26984091197311decd9028eMichael Han
151307fc1ba7553f2f5bf26984091197311decd9028eMichael Han      // Recover by adding misplaced attributes to the attribute list
151407fc1ba7553f2f5bf26984091197311decd9028eMichael Han      // of the class so they can be applied on the class later.
151507fc1ba7553f2f5bf26984091197311decd9028eMichael Han      attrs.takeAllFrom(Attributes);
151607fc1ba7553f2f5bf26984091197311decd9028eMichael Han    }
151707fc1ba7553f2f5bf26984091197311decd9028eMichael Han  }
15182e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
151913489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  // If this is an elaborated type specifier, and we delayed
152013489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  // diagnostics before, just merge them into the current pool.
152113489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  if (shouldDelayDiagsInTag) {
152213489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall    diagsFromTag.done();
152313489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall    if (TUK == Sema::TUK_Reference)
152413489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall      diagsFromTag.redelay();
152513489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall  }
152613489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall
1527207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall  if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
1528f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                               TUK != Sema::TUK_Definition)) {
1529207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall    if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1530207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall      // We have a declaration or reference to an anonymous class.
1531207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall      Diag(StartLoc, diag::err_anon_type_definition)
1532651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        << DeclSpec::getSpecifierName(TagType, Policy);
1533207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall    }
1534e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1535e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling    // If we are parsing a definition and stop at a base-clause, continue on
1536e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling    // until the semicolon.  Continuing from the comma will just trick us into
1537e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling    // thinking we are seeing a variable declaration.
1538e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling    if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1539e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling      SkipUntil(tok::semi, StopBeforeMatch);
1540e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling    else
1541e78e8fc27140309092fb56d77a72f31fa085f9daBill Wendling      SkipUntil(tok::comma, StopAtSemi);
1542e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    return;
1543e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  }
1544e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1545ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  // Create the tag portion of the class or class template.
1546d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  DeclResult TagOrTempResult = true; // invalid
1547d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  TypeResult TypeResult = true; // invalid
15484d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor
1549402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  bool Owned = false;
1550f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  if (TemplateId) {
15514d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor    // Explicit specialization, class template partial specialization,
15524d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor    // or explicit instantiation.
15535354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
155439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor                                       TemplateId->NumArgs);
15554d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor    if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
1556f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        TUK == Sema::TUK_Declaration) {
15574d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      // This is an explicit instantiation of a class template.
15582edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      ProhibitAttributes(attrs);
15592edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
15604d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      TagOrTempResult
156123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        = Actions.ActOnExplicitInstantiation(getCurScope(),
156245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor                                             TemplateInfo.ExternLoc,
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             TemplateInfo.TemplateLoc,
15644d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor                                             TagType,
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             StartLoc,
15664d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor                                             SS,
15672b5289b6fd7e3d9899868410a498c081c9595662John McCall                                             TemplateId->Template,
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             TemplateId->TemplateNameLoc,
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             TemplateId->LAngleLoc,
15704d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor                                             TemplateArgsPtr,
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             TemplateId->RAngleLoc,
15727f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                             attrs.getList());
157374256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall
157474256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall    // Friend template-ids are treated as references unless
157574256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall    // they have template headers, in which case they're ill-formed
157674256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall    // (FIXME: "template <class T> friend class A<T>::B<int>;").
157774256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall    // We diagnose this error in ActOnClassTemplateSpecialization.
1578f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    } else if (TUK == Sema::TUK_Reference ||
1579f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall               (TUK == Sema::TUK_Friend &&
158074256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall                TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
15812edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      ProhibitAttributes(attrs);
158255d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara      TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
1583059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                                  TemplateId->SS,
158455d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                                  TemplateId->TemplateKWLoc,
1585059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                                  TemplateId->Template,
1586059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                                  TemplateId->TemplateNameLoc,
1587059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                                  TemplateId->LAngleLoc,
1588059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                                  TemplateArgsPtr,
158955d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                                  TemplateId->RAngleLoc);
15904d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor    } else {
15914d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      // This is an explicit specialization or a class template
15924d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      // partial specialization.
15934d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      TemplateParameterLists FakedParamLists;
15944d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
15954d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        // This looks like an explicit instantiation, because we have
15964d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        // something like
15974d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        //
15984d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        //   template class Foo<X>
15994d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        //
16003f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor        // but it actually has a definition. Most likely, this was
16014d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        // meant to be an explicit specialization, but the user forgot
16024d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor        // the '<>' after 'template'.
160361dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith        // It this is friend declaration however, since it cannot have a
160461dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith        // template header, it is most likely that the user meant to
160561dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith        // remove the 'template' keyword.
16064985429d6f0dddbe168ec0ed4de029d56294e644Larisse Voufo        assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
160761dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith               "Expected a definition here");
160861dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith
160961dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith        if (TUK == Sema::TUK_Friend) {
161061dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
16116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          TemplateParams = nullptr;
161261dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith        } else {
161361dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          SourceLocation LAngleLoc =
161461dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith              PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
161561dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          Diag(TemplateId->TemplateNameLoc,
161661dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith               diag::err_explicit_instantiation_with_definition)
161761dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith              << SourceRange(TemplateInfo.TemplateLoc)
161861dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith              << FixItHint::CreateInsertion(LAngleLoc, "<>");
161961dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith
162061dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          // Create a fake template parameter list that contains only
162161dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          // "template<>", so that we treat this construct as a class
162261dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          // template specialization.
162361dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
16246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
16256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              0, LAngleLoc));
162661dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith          TemplateParams = &FakedParamLists;
162761dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith        }
16284d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      }
16294d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor
16304d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor      // Build the class template specialization.
16316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
16326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
16336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          *TemplateId, attrs.getList(),
16346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
16356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                : nullptr,
16366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 TemplateParams ? TemplateParams->size() : 0));
16374d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor    }
16383f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor  } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
1639f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall             TUK == Sema::TUK_Declaration) {
16403f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    // Explicit instantiation of a member of a class template
16413f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    // specialization, e.g.,
16423f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    //
16433f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    //   template struct Outer<int>::Inner;
16443f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    //
16452edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
16462edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
16473f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    TagOrTempResult
164823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      = Actions.ActOnExplicitInstantiation(getCurScope(),
164945f965581935791a018df829a14dff53c1dd8f47Douglas Gregor                                           TemplateInfo.ExternLoc,
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           TemplateInfo.TemplateLoc,
16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           TagType, StartLoc, SS, Name,
16527f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                           NameLoc, attrs.getList());
16539a34edb710917798aa30263374f624f13b594605John McCall  } else if (TUK == Sema::TUK_Friend &&
16549a34edb710917798aa30263374f624f13b594605John McCall             TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
16552edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
16562edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
16579a34edb710917798aa30263374f624f13b594605John McCall    TagOrTempResult =
16589a34edb710917798aa30263374f624f13b594605John McCall      Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
16599a34edb710917798aa30263374f624f13b594605John McCall                                      TagType, StartLoc, SS,
16607f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                      Name, NameLoc, attrs.getList(),
16615354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                      MultiTemplateParamsArg(
16626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                    TemplateParams? &(*TemplateParams)[0]
16636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                  : nullptr,
16649a34edb710917798aa30263374f624f13b594605John McCall                                 TemplateParams? TemplateParams->size() : 0));
16653f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor  } else {
16662edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
16672edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      ProhibitAttributes(attrs);
166861dfea9d34aaf7ea1ba6c28d8dd44a1d7cf40f78Richard Smith
16697c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo    if (TUK == Sema::TUK_Definition &&
16707c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo        TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
16717c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo      // If the declarator-id is not a template-id, issue a diagnostic and
16727c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo      // recover by ignoring the 'template' keyword.
16737c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo      Diag(Tok, diag::err_template_defn_explicit_instantiation)
16747c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo        << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
16756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TemplateParams = nullptr;
16767c64ef05e179d29646030e9d453081844ecc537aLarisse Voufo    }
16772edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1678c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall    bool IsDependent = false;
1679c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall
1680a25c4080a490ea2bab6f54094dd75b19eae83770John McCall    // Don't pass down template parameter lists if this is just a tag
1681a25c4080a490ea2bab6f54094dd75b19eae83770John McCall    // reference.  For example, we don't need the template parameters here:
1682a25c4080a490ea2bab6f54094dd75b19eae83770John McCall    //   template <class T> class A *makeA(T t);
1683a25c4080a490ea2bab6f54094dd75b19eae83770John McCall    MultiTemplateParamsArg TParams;
1684a25c4080a490ea2bab6f54094dd75b19eae83770John McCall    if (TUK != Sema::TUK_Reference && TemplateParams)
1685a25c4080a490ea2bab6f54094dd75b19eae83770John McCall      TParams =
1686a25c4080a490ea2bab6f54094dd75b19eae83770John McCall        MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1687a25c4080a490ea2bab6f54094dd75b19eae83770John McCall
16883f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor    // Declaration or definition of a class type
16899a34edb710917798aa30263374f624f13b594605John McCall    TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
16907f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                       SS, Name, NameLoc, attrs.getList(), AS,
1691e761230ae3751b525cadd8066c74ec278ee4ef57Douglas Gregor                                       DS.getModulePrivateSpecLoc(),
1692bdad7a2e21686296b78dac6190b78d11c996f6d7Richard Smith                                       TParams, Owned, IsDependent,
1693bdad7a2e21686296b78dac6190b78d11c996f6d7Richard Smith                                       SourceLocation(), false,
1694651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                       clang::TypeResult(),
1695651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                       DSC == DSC_type_specifier);
1696c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall
1697c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall    // If ActOnTag said the type was dependent, try again with the
1698c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall    // less common call.
16999a34edb710917798aa30263374f624f13b594605John McCall    if (IsDependent) {
17009a34edb710917798aa30263374f624f13b594605John McCall      assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
170123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
1702193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam                                             SS, Name, StartLoc, NameLoc);
17039a34edb710917798aa30263374f624f13b594605John McCall    }
17043f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor  }
1705e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1706e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // If there is a body, parse it and inform the actions module.
1707f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TUK == Sema::TUK_Definition) {
1708bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall    assert(Tok.is(tok::l_brace) ||
17094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
17104e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith           isCXX11FinalKeyword());
17114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus)
171207fc1ba7553f2f5bf26984091197311decd9028eMichael Han      ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
171307fc1ba7553f2f5bf26984091197311decd9028eMichael Han                                  TagOrTempResult.get());
171407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    else
1715212e81cc5151b3c42346e43cfd42499a53ffd39aDouglas Gregor      ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
1716e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  }
1717e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
17186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const char *PrevSpec = nullptr;
1719b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  unsigned DiagID;
1720b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  bool Result;
1721c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall  if (!TypeResult.isInvalid()) {
17220daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
17230daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara                                NameLoc.isValid() ? NameLoc : StartLoc,
1724651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                PrevSpec, DiagID, TypeResult.get(), Policy);
1725c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall  } else if (!TagOrTempResult.isInvalid()) {
17260daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = DS.SetTypeSpecType(TagType, StartLoc,
17270daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara                                NameLoc.isValid() ? NameLoc : StartLoc,
1728651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1729651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                Policy);
1730c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall  } else {
1731ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor    DS.SetTypeSpecError();
173266e9977ddd6b197317d149213b76a9af0d3df4deAnders Carlsson    return;
173366e9977ddd6b197317d149213b76a9af0d3df4deAnders Carlsson  }
17341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1735b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (Result)
1736fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    Diag(StartLoc, DiagID) << PrevSpec;
1737193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
17384ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  // At this point, we've successfully parsed a class-specifier in 'definition'
17394ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  // form (e.g. "struct foo { int x; }".  While we could just return here, we're
17404ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  // going to look at what comes after it to improve error recovery.  If an
17414ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  // impossible token occurs next, we assume that the programmer forgot a ; at
17424ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  // the end of the declaration and recover that way.
17434ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  //
1744c9f351700721150a985f21844fbfec55b04e861dRichard Smith  // Also enforce C++ [temp]p3:
1745c9f351700721150a985f21844fbfec55b04e861dRichard Smith  //   In a template-declaration which defines a class, no declarator
1746c9f351700721150a985f21844fbfec55b04e861dRichard Smith  //   is permitted.
1747176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  //
1748176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // After a type-specifier, we don't expect a semicolon. This only happens in
1749176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // C, since definitions are not permitted in this context in C++.
175017d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  if (TUK == Sema::TUK_Definition &&
1751176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
175217d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos      (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
17537d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis    if (Tok.isNot(tok::semi)) {
1754651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
1755651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ExpectAndConsume(tok::semi, diag::err_expected_after,
1756651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       DeclSpec::getSpecifierName(TagType, PPol));
17577d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis      // Push this token back into the preprocessor and change our current token
17587d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis      // to ';' so that the rest of the code recovers as though there were an
17597d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis      // ';' after the definition.
17607d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis      PP.EnterToken(Tok);
17617d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis      Tok.setKind(tok::semi);
17627d033b209e87d1964ee2f8de668934bb1a77bde0Argyrios Kyrtzidis    }
17634ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner  }
1764e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor}
1765e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
17661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
1767e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1768e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       base-clause : [C++ class.derived]
1769e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         ':' base-specifier-list
1770e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       base-specifier-list:
1771e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         base-specifier '...'[opt]
1772e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         base-specifier-list ',' base-specifier '...'[opt]
1773d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseBaseClause(Decl *ClassDecl) {
1774e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  assert(Tok.is(tok::colon) && "Not a base clause");
1775e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  ConsumeToken();
1776e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1777f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor  // Build up an array of parsed base specifiers.
17785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
1779f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor
1780e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  while (true) {
1781e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    // Parse a base-specifier.
1782f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor    BaseResult Result = ParseBaseSpecifier(ClassDecl);
17835ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor    if (Result.isInvalid()) {
1784e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor      // Skip the rest of this base specifier, up until the comma or
1785e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor      // opening brace.
17868fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
1787f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor    } else {
1788f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor      // Add this to our array of base specifiers.
17895ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor      BaseInfo.push_back(Result.get());
1790e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    }
1791e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1792e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    // If the next token is a comma, consume it and keep reading
1793e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    // base-specifiers.
1794651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!TryConsumeToken(tok::comma))
1795651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      break;
1796e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  }
1797f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor
1798f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor  // Attach the base specifiers
1799beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
1800e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor}
1801e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1802e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1803e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// one entry in the base class list of a class specifier, for example:
1804e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///    class foo : public bar, virtual private baz {
1805e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'public bar' and 'virtual private baz' are each base-specifiers.
1806e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1807e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       base-specifier: [C++ class.derived]
1808053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///         attribute-specifier-seq[opt] base-type-specifier
1809053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///         attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1810053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///                 base-type-specifier
1811053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///         attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1812053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith///                 base-type-specifier
1813176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesBaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
1814e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  bool IsVirtual = false;
1815e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  SourceLocation StartLoc = Tok.getLocation();
1816e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1817053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith  ParsedAttributesWithRange Attributes(AttrFactory);
1818053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith  MaybeParseCXX11Attributes(Attributes);
1819053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith
1820e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // Parse the 'virtual' keyword.
1821651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TryConsumeToken(tok::kw_virtual))
1822e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    IsVirtual = true;
1823e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1824053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith  CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1825053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith
1826e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // Parse an (optional) access specifier.
1827e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  AccessSpecifier Access = getAccessSpecifierIfPresent();
182892f883177b162928a8e632e4e3b93fafd2b26072John McCall  if (Access != AS_none)
1829e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    ConsumeToken();
18301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1831053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith  CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1832053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith
1833e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // Parse the 'virtual' keyword (again!), in case it came after the
1834e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // access specifier.
1835e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  if (Tok.is(tok::kw_virtual))  {
1836e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    SourceLocation VirtualLoc = ConsumeToken();
1837e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    if (IsVirtual) {
1838e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor      // Complain about duplicate 'virtual'
18391ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      Diag(VirtualLoc, diag::err_dup_virtual)
1840849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(VirtualLoc);
1841e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    }
1842e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1843e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor    IsVirtual = true;
1844e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  }
1845e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1846053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith  CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1847053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith
184842a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor  // Parse the class-name.
18497f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor  SourceLocation EndLocation;
185022216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie  SourceLocation BaseLoc;
185122216eb4fb0936d2488fc03abd285d135c36ff01David Blaikie  TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
185231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor  if (BaseType.isInvalid())
185342a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor    return true;
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1855f90b27ad077c3339b62befc892382845339f9490Douglas Gregor  // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1856f90b27ad077c3339b62befc892382845339f9490Douglas Gregor  // actually part of the base-specifier-list grammar productions, but we
1857f90b27ad077c3339b62befc892382845339f9490Douglas Gregor  // parse it here for convenience.
1858f90b27ad077c3339b62befc892382845339f9490Douglas Gregor  SourceLocation EllipsisLoc;
1859651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  TryConsumeToken(tok::ellipsis, EllipsisLoc);
1860651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
18611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Find the complete source range for the base-specifier.
18627f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor  SourceRange Range(StartLoc, EndLocation);
18631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1864e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // Notify semantic analysis that we have parsed a complete
1865e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  // base-specifier.
1866053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith  return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1867053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith                                    Access, BaseType.get(), BaseLoc,
1868053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith                                    EllipsisLoc);
1869e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor}
1870e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor
1871e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// getAccessSpecifierIfPresent - Determine whether the next token is
1872e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// a C++ access-specifier.
1873e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///
1874e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///       access-specifier: [C++ class.derived]
1875e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'private'
1876e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'protected'
1877e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor///         'public'
18781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpAccessSpecifier Parser::getAccessSpecifierIfPresent() const {
1879e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  switch (Tok.getKind()) {
1880e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  default: return AS_none;
1881e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  case tok::kw_private: return AS_private;
1882e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  case tok::kw_protected: return AS_protected;
1883e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  case tok::kw_public: return AS_public;
1884e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor  }
1885e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor}
18864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
188774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor/// \brief If the given declarator has any parts for which parsing has to be
1888176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// delayed, e.g., default arguments or an exception-specification, create a
1889176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// late-parsed method declaration record to handle the parsing at the end of
1890176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// the class definition.
189174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregorvoid Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
189274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                            Decl *ThisDecl) {
18931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclaratorChunk::FunctionTypeInfo &FTI
1894075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    = DeclaratorInfo.getFunctionTypeInfo();
18950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // If there was a late-parsed exception-specification, we'll need a
18960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // late parse
18970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
18980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
18990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (!NeedLateParse) {
19000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // Look ahead to see if there are any default args
19010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
19020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
19030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Param->hasUnparsedDefaultArg()) {
19040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        NeedLateParse = true;
19050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        break;
19060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
19070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
19080e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
190974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor
19100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (NeedLateParse) {
1911176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Push this method onto the stack of late-parsed method
1912176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // declarations.
19130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1914176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1915176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1916176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
19170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // Stash the exception-specification tokens in the late-pased method.
1918176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1919176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    FTI.ExceptionSpecTokens = 0;
1920176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
19210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // Push tokens for each parameter.  Those that do not have
19220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // defaults will be NULL.
1923176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    LateMethod->DefaultArgs.reserve(FTI.NumParams);
19240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
1925651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
19260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
1927d33133cdc1af466f9c276249b2621be03867888bEli Friedman  }
1928d33133cdc1af466f9c276249b2621be03867888bEli Friedman}
1929d33133cdc1af466f9c276249b2621be03867888bEli Friedman
19304e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
19311f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson/// virt-specifier.
19321f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///
19331f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///       virt-specifier:
19341f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         override
19351f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         final
19364e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard SmithVirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
1937651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
1938cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson    return VirtSpecifiers::VS_None;
1939cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson
1940651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  IdentifierInfo *II = Tok.getIdentifierInfo();
19417eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson
1942651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Initialize the contextual keywords.
1943651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!Ident_final) {
1944651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Ident_final = &PP.getIdentifierTable().get("final");
1945651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (getLangOpts().MicrosoftExt)
1946651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Ident_sealed = &PP.getIdentifierTable().get("sealed");
1947651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Ident_override = &PP.getIdentifierTable().get("override");
1948651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
19491f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
1950651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (II == Ident_override)
1951651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return VirtSpecifiers::VS_Override;
19527121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer
1953651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (II == Ident_sealed)
1954651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return VirtSpecifiers::VS_Sealed;
1955651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1956651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (II == Ident_final)
1957651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return VirtSpecifiers::VS_Final;
1958b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson
1959b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson  return VirtSpecifiers::VS_None;
19601f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson}
19611f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
19624e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
19631f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///
19641f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///       virt-specifier-seq:
19651f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         virt-specifier
19661f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         virt-specifier-seq virt-specifier
19674e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smithvoid Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
1968176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                                bool IsInterface,
1969176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                                SourceLocation FriendLoc) {
1970b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson  while (true) {
19714e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1972b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson    if (Specifier == VirtSpecifiers::VS_None)
1973b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson      return;
1974b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson
1975176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (FriendLoc.isValid()) {
1976176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1977176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        << VirtSpecifiers::getSpecifierName(Specifier)
1978176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        << FixItHint::CreateRemoval(Tok.getLocation())
1979176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        << SourceRange(FriendLoc, FriendLoc);
1980176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ConsumeToken();
1981176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      continue;
1982176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
1983176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
1984b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson    // C++ [class.mem]p8:
1985b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson    //   A virt-specifier-seq shall contain at most one of each virt-specifier.
19866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const char *PrevSpec = nullptr;
198746127a96b6dd6b93aa18d5f7a55bc2db8b52a2c9Anders Carlsson    if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
1988b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson      Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1989b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson        << PrevSpec
1990b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson        << FixItHint::CreateRemoval(Tok.getLocation());
1991b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson
19927121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
19937121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer                        Specifier == VirtSpecifiers::VS_Sealed)) {
1994e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall      Diag(Tok.getLocation(), diag::err_override_control_interface)
1995e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        << VirtSpecifiers::getSpecifierName(Specifier);
19967121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    } else if (Specifier == VirtSpecifiers::VS_Sealed) {
19977121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer      Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
1998e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall    } else {
19997121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer      Diag(Tok.getLocation(),
20007121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer           getLangOpts().CPlusPlus11
20017121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer               ? diag::warn_cxx98_compat_override_control_keyword
20027121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer               : diag::ext_override_control_keyword)
20037121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer          << VirtSpecifiers::getSpecifierName(Specifier);
2004e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall    }
2005b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson    ConsumeToken();
2006b971dbdb65149a7cf0c046380186d0204e5b411eAnders Carlsson  }
20071f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson}
20081f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
20094e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith/// isCXX11FinalKeyword - Determine whether the next token is a C++11
2010651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// 'final' or Microsoft 'sealed' contextual keyword.
20114e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smithbool Parser::isCXX11FinalKeyword() const {
2012651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2013651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return Specifier == VirtSpecifiers::VS_Final ||
2014651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         Specifier == VirtSpecifiers::VS_Sealed;
2015651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
2016cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson
2017651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// \brief Parse a C++ member-declarator up to, but not including, the optional
2018651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// brace-or-equal-initializer or pure-specifier.
20190e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesbool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
2020651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2021651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    LateParsedAttrList &LateParsedAttrs) {
2022651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // member-declarator:
2023651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  //   declarator pure-specifier[opt]
2024651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  //   declarator brace-or-equal-initializer[opt]
2025651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  //   identifier[opt] ':' constant-expression
2026176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (Tok.isNot(tok::colon))
2027651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ParseDeclarator(DeclaratorInfo);
2028176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  else
2029176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
2030cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson
2031651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
2032176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    assert(DeclaratorInfo.isPastIdentifier() &&
2033176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           "don't know where identifier would go yet?");
2034651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    BitfieldSize = ParseConstantExpression();
2035651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (BitfieldSize.isInvalid())
2036651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2037651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else
2038176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ParseOptionalCXX11VirtSpecifierSeq(
2039176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        VS, getCurrentClass().IsInterface,
2040176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2041651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2042651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // If a simple-asm-expr is present, parse it.
2043651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Tok.is(tok::kw_asm)) {
2044651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SourceLocation Loc;
2045651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2046651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (AsmLabel.isInvalid())
2047651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2048651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2049c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    DeclaratorInfo.setAsmLabel(AsmLabel.get());
2050651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DeclaratorInfo.SetRangeEnd(Loc);
2051cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson  }
20527121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer
2053651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // If attributes exist after the declarator, but before an '{', parse them.
2054651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
2055651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2056651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // For compatibility with code written to older Clang, also accept a
2057651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // virt-specifier *after* the GNU attributes.
2058176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (BitfieldSize.isUnset() && VS.isUnset()) {
2059176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ParseOptionalCXX11VirtSpecifierSeq(
2060176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        VS, getCurrentClass().IsInterface,
2061176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2062176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (!VS.isUnset()) {
2063176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // If we saw any GNU-style attributes that are known to GCC followed by a
2064176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // virt-specifier, issue a GCC-compat warning.
2065176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      const AttributeList *Attr = DeclaratorInfo.getAttributes();
2066176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      while (Attr) {
2067176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2068176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2069176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        Attr = Attr->getNext();
2070176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
2071176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
2072176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
20730e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
20740e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // If this has neither a name nor a bit width, something has gone seriously
20750e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // wrong. Skip until the semi-colon or }.
20760e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
20770e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // If so, skip until the semi-colon or a }.
20780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
20790e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return true;
20800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
20810e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  return false;
2082cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson}
2083cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson
20844cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
20854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
20864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///       member-declaration:
20874cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         decl-specifier-seq[opt] member-declarator-list[opt] ';'
20884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         function-definition ';'[opt]
20894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
20904cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         using-declaration                                            [TODO]
2091511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// [C++0x] static_assert-declaration
20925aeccdbb4bdc94e48c04cacc59fa812af32109b2Anders Carlsson///         template-declaration
2093bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner/// [GNU]   '__extension__' member-declaration
20944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
20954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///       member-declarator-list:
20964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         member-declarator
20974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         member-declarator-list ',' member-declarator
20984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
20994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///       member-declarator:
21001f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         declarator virt-specifier-seq[opt] pure-specifier[opt]
21014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         declarator constant-initializer[opt]
21027a614d8380297fcd2bc23986241905d97222948cRichard Smith/// [C++11] declarator brace-or-equal-initializer[opt]
21034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         identifier[opt] ':' constant-expression
21044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
21051f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///       virt-specifier-seq:
21061f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         virt-specifier
21071f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         virt-specifier-seq virt-specifier
21081f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///
21091f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///       virt-specifier:
21101f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         override
21111f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///         final
21127121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer/// [MS]    sealed
21131f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson///
2114e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl///       pure-specifier:
21154cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         '= 0'
21164cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
21174cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///       constant-initializer:
21184cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         '=' constant-expression
21194cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
212037b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregorvoid Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
21215f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen                                            AttributeList *AccessAttrs,
2122c9068d7dd94d439cec66c421115d15303e481025John McCall                                       const ParsedTemplateInfo &TemplateInfo,
2123c9068d7dd94d439cec66c421115d15303e481025John McCall                                       ParsingDeclRAIIObject *TemplateDiags) {
21248a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor  if (Tok.is(tok::at)) {
21254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
21268a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor      Diag(Tok, diag::err_at_defs_cxx);
21278a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor    else
21288a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor      Diag(Tok, diag::err_at_in_class);
2129b310439121c875937d78cc49cc969bc1197fc025Richard Smith
21308a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor    ConsumeToken();
21318fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace, StopAtSemi);
21328a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor    return;
21338a9013d24864272cf5b18c908a267bd7a2bda4c4Douglas Gregor  }
2134b310439121c875937d78cc49cc969bc1197fc025Richard Smith
2135176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // Turn on colon protection early, while parsing declspec, although there is
2136176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // nothing to protect there. It prevents from false errors if error recovery
2137176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // incorrectly determines where the declspec ends, as in the example:
2138176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  //   struct A { enum class B { C }; };
2139176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  //   const int C = 4;
2140176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  //   struct D { A::B : C; };
2141176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ColonProtectionRAIIObject X(*this);
2142176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
214360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall  // Access declarations.
214483a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith  bool MalformedTypeSpec = false;
214560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall  if (!TemplateInfo.Kind &&
2146176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2147176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines       Tok.is(tok::kw___super))) {
214883a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith    if (TryAnnotateCXXScopeToken())
214983a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith      MalformedTypeSpec = true;
215083a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith
215183a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith    bool isAccessDecl;
215283a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith    if (Tok.isNot(tok::annot_cxxscope))
215383a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith      isAccessDecl = false;
215483a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith    else if (NextToken().is(tok::identifier))
215560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      isAccessDecl = GetLookAheadToken(2).is(tok::semi);
215660fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall    else
215760fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      isAccessDecl = NextToken().is(tok::kw_operator);
215860fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall
215960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall    if (isAccessDecl) {
216060fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      // Collect the scope specifier token we annotated earlier.
216160fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      CXXScopeSpec SS;
2162efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor      ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2163efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                     /*EnteringContext=*/false);
216460fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall
2165176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (SS.isInvalid()) {
2166176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        SkipUntil(tok::semi);
2167176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return;
2168176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
2169176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
217060fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      // Try to parse an unqualified-id.
2171e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      SourceLocation TemplateKWLoc;
217260fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      UnqualifiedId Name;
2173e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2174e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                             TemplateKWLoc, Name)) {
217560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall        SkipUntil(tok::semi);
217660fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall        return;
217760fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      }
217860fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall
217960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      // TODO: recover from mistakenly-qualified operator declarations.
2180651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2181651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           "access declaration")) {
2182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        SkipUntil(tok::semi);
218360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall        return;
2184651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      }
218560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall
218623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnUsingDeclaration(getCurScope(), AS,
21878d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                    /* HasUsingKeyword */ false,
21888d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                    SourceLocation(),
218960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall                                    SS, Name,
21906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                    /* AttrList */ nullptr,
21918d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                    /* HasTypenameKeyword */ false,
219260fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall                                    SourceLocation());
219360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall      return;
219460fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall    }
219560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall  }
219660fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall
2197176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // static_assert-declaration. A templated static_assert declaration is
2198176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2199176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (!TemplateInfo.Kind &&
2200176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
220197144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    SourceLocation DeclEnd;
220297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    ParseStaticAssertDeclaration(DeclEnd);
2203682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return;
2204682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
22051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2206682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  if (Tok.is(tok::kw_template)) {
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(!TemplateInfo.TemplateParams &&
220837b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor           "Nested template improperly parsed?");
220997144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    SourceLocation DeclEnd;
22101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
22115f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen                                         AS, AccessAttrs);
2212682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return;
2213682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
22145aeccdbb4bdc94e48c04cacc59fa812af32109b2Anders Carlsson
2215bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner  // Handle:  member-declaration ::= '__extension__' member-declaration
2216bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner  if (Tok.is(tok::kw___extension__)) {
2217bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner    // __extension__ silences extension warnings in the subexpression.
2218bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
2219bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner    ConsumeToken();
22205f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen    return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
22215f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen                                          TemplateInfo, TemplateDiags);
2222bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner  }
22239cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
22240b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
222552b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  ParsedAttributesWithRange FnAttrs(AttrFactory);
22264e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  // Optional C++11 attribute-specifier
22274e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
222852b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  // We need to keep these attributes for future diagnostic
222952b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  // before they are taken over by declaration specifier.
223052b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  FnAttrs.addAll(attrs.getList());
223152b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  FnAttrs.Range = attrs.Range;
223252b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han
22337f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
2234bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
22359cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  if (Tok.is(tok::kw_using)) {
22367f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
22371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22389cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    // Eat 'using'.
22399cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    SourceLocation UsingLoc = ConsumeToken();
22409cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
22419cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    if (Tok.is(tok::kw_namespace)) {
22429cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor      Diag(UsingLoc, diag::err_using_namespace_in_class);
22438fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopBeforeMatch);
2244ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner    } else {
22459cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor      SourceLocation DeclEnd;
22463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      // Otherwise, it must be a using-declaration or an alias-declaration.
224778b810559d89e996e00684335407443936ce34a1John McCall      ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
224878b810559d89e996e00684335407443936ce34a1John McCall                            UsingLoc, DeclEnd, AS);
22499cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    }
22509cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    return;
22519cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  }
22529cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
22532287c5e2352fc51cd74e8a9a7725cbf87e41c007DeLesley Hutchins  // Hold late-parsed attributes so we can attach a Decl to them later.
22542287c5e2352fc51cd74e8a9a7725cbf87e41c007DeLesley Hutchins  LateParsedAttrList CommonLateParsedAttrs;
22552287c5e2352fc51cd74e8a9a7725cbf87e41c007DeLesley Hutchins
22564cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // decl-specifier-seq:
22574cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // Parse the common declaration-specifiers piece.
2258c9068d7dd94d439cec66c421115d15303e481025John McCall  ParsingDeclSpec DS(*this, TemplateDiags);
22597f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  DS.takeAttributesFrom(attrs);
226083a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith  if (MalformedTypeSpec)
226183a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith    DS.SetTypeSpecError();
2262651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2263176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                             &CommonLateParsedAttrs);
2265176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2266176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // Turn off colon protection that was set for declspec.
2267176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  X.restore();
22684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
2269f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling  // If we had a free-standing type definition with a missing semicolon, we
2270f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling  // may get this far before the problem becomes obvious.
2271f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling  if (DS.hasTagDefinition() &&
2272f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling      TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2273f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling      DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2274f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling                                            &CommonLateParsedAttrs))
2275f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling    return;
2276f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling
22775354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  MultiTemplateParamsArg TemplateParams(
22786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
22796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 : nullptr,
2280dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall      TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2281dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
2282651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TryConsumeToken(tok::semi)) {
228352b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han    if (DS.isFriendSpecified())
228452b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      ProhibitAttributes(FnAttrs);
228552b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han
2286d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl =
22870f4be74ff0273e505d383f89174ef539828424edChandler Carruth      Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
2288c9068d7dd94d439cec66c421115d15303e481025John McCall    DS.complete(TheDecl);
228967d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall    return;
22904cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
229107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis
229254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
22934867347e82648d3baf09524b98b09c297a5a198fNico Weber  VirtSpecifiers VS;
22944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
2295eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski  // Hold late-parsed attributes so we can attach a Decl to them later.
2296eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski  LateParsedAttrList LateParsedAttrs;
2297eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski
2298a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor  SourceLocation EqualLoc;
2299a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor  bool HasInitializer = false;
2300a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor  ExprResult Init;
2301a1efc8c8c6460d860d6509b05b341e77ed9bfe87Chris Lattner
2302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SmallVector<Decl *, 8> DeclsInGroup;
2303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ExprResult BitfieldSize;
2304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ExpectSemi = true;
23054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
2306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Parse the first declarator.
23070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (ParseCXXMemberDeclaratorBeforeInitializer(
23080e2c34f92f00628d48968dfea096d36381f494cbStephen Hines          DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
2309651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    TryConsumeToken(tok::semi);
2310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return;
2311651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
23121b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson
2313651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Check for a member function definition.
2314651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (BitfieldSize.isUnset()) {
2315651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // MSVC permits pure specifier on inline functions defined at class scope.
23166a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet    // Hence check for =0 before checking for function definition.
23174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
2318651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        DeclaratorInfo.isFunctionDeclarator() &&
23196a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet        NextToken().is(tok::numeric_constant)) {
2320a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor      EqualLoc = ConsumeToken();
23216a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet      Init = ParseInitializer();
23226a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet      if (Init.isInvalid())
23238fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2324a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor      else
2325a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor        HasInitializer = true;
23266a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet    }
23276a24747beed797b2f1184c66ca45beb4db20eb08Francois Pichet
232845fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor    FunctionDefinitionKind DefinitionKind = FDK_Declaration;
23293a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis    // function-definition:
23307a614d8380297fcd2bc23986241905d97222948cRichard Smith    //
23317a614d8380297fcd2bc23986241905d97222948cRichard Smith    // In C++11, a non-function declarator followed by an open brace is a
23327a614d8380297fcd2bc23986241905d97222948cRichard Smith    // braced-init-list for an in-class member initialization, not an
23337a614d8380297fcd2bc23986241905d97222948cRichard Smith    // erroneous function definition.
233480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
233545fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor      DefinitionKind = FDK_Definition;
2336e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else if (DeclaratorInfo.isFunctionDeclarator()) {
23377a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
233845fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor        DefinitionKind = FDK_Definition;
2339e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      } else if (Tok.is(tok::equal)) {
2340e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        const Token &KW = NextToken();
234145fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor        if (KW.is(tok::kw_default))
234245fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor          DefinitionKind = FDK_Defaulted;
234345fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor        else if (KW.is(tok::kw_delete))
234445fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor          DefinitionKind = FDK_Deleted;
2345e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      }
2346e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
2347e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
234852b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han    // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
234952b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han    // to a friend declaration, that declaration shall be a definition.
235052b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han    if (DeclaratorInfo.isFunctionDeclarator() &&
235152b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han        DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
235252b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      // Diagnose attributes that appear before decl specifier:
235352b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      // [[]] friend int foo();
235452b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      ProhibitAttributes(FnAttrs);
235552b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han    }
235652b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han
23570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (DefinitionKind != FDK_Declaration) {
23583a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis      if (!DeclaratorInfo.isFunctionDeclarator()) {
235965ba94814f667e6ea1fcbf0896ad496bb7010335Richard Trieu        Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
23603a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis        ConsumeBrace();
23618fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::r_brace);
236252b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han
23639ea416e598fa3cb09d67d514c4519c99abb81321Douglas Gregor        // Consume the optional ';'
2364651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        TryConsumeToken(tok::semi);
2365651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2366682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner        return;
23673a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis      }
23683a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis
23693a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis      if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
237065ba94814f667e6ea1fcbf0896ad496bb7010335Richard Trieu        Diag(DeclaratorInfo.getIdentifierLoc(),
237165ba94814f667e6ea1fcbf0896ad496bb7010335Richard Trieu             diag::err_function_declared_typedef);
23729ea416e598fa3cb09d67d514c4519c99abb81321Douglas Gregor
23736f9a445760992a6fbff2c0b08becf35ae9eafa71Richard Smith        // Recover by treating the 'typedef' as spurious.
23746f9a445760992a6fbff2c0b08becf35ae9eafa71Richard Smith        DS.ClearStorageClassSpecs();
23753a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis      }
23764cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
2377eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski      Decl *FunDecl =
23785f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen        ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
237945fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor                                VS, DefinitionKind, Init);
2380eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski
2381fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer      if (FunDecl) {
2382fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2383fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer          CommonLateParsedAttrs[i]->addDecl(FunDecl);
2384fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        }
2385fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2386fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer          LateParsedAttrs[i]->addDecl(FunDecl);
2387fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        }
2388eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski      }
2389eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski      LateParsedAttrs.clear();
2390e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
2391e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      // Consume the ';' - it's optional unless we have a delete or default
23924b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu      if (Tok.is(tok::semi))
2393eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        ConsumeExtraSemi(AfterMemberFunctionDefinition);
23949ea416e598fa3cb09d67d514c4519c99abb81321Douglas Gregor
2395682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return;
23963a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis    }
23974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
23984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
23994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // member-declarator-list:
24004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  //   member-declarator
24014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  //   member-declarator-list ',' member-declarator
24024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
24034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  while (1) {
2404ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith    InClassInitStyle HasInClassInit = ICIS_NoInit;
2405a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor    if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
24067a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (BitfieldSize.get()) {
24077a614d8380297fcd2bc23986241905d97222948cRichard Smith        Diag(Tok, diag::err_bitfield_member_init);
24088fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
24097a614d8380297fcd2bc23986241905d97222948cRichard Smith      } else {
2410147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor        HasInitializer = true;
2411ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith        if (!DeclaratorInfo.isDeclarationOfFunction() &&
2412ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith            DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2413ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith              != DeclSpec::SCS_typedef)
2414ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith          HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
24157a614d8380297fcd2bc23986241905d97222948cRichard Smith      }
24167a614d8380297fcd2bc23986241905d97222948cRichard Smith    }
24177a614d8380297fcd2bc23986241905d97222948cRichard Smith
241807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    // NOTE: If Sema is the Action module and declarator is an instance field,
2419682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    // this call will *not* return the created decl; It will return null.
242007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    // See Sema::ActOnCXXMemberDeclarator for details.
242167d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall
24226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NamedDecl *ThisDecl = nullptr;
242367d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall    if (DS.isFriendSpecified()) {
2424651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
242552b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      // to a friend declaration, that declaration shall be a definition.
242652b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      //
2427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // Diagnose attributes that appear in a friend member function declarator:
2428651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      //   friend int foo [[]] ();
242952b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      SmallVector<SourceRange, 4> Ranges;
243052b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han      DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2431651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2432651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           E = Ranges.end(); I != E; ++I)
2433651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
243452b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han
243523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
24363fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                                 TemplateParams);
243737b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor    } else {
243823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
243967d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall                                                  DeclaratorInfo,
24403fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                                  TemplateParams,
2441c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                                  BitfieldSize.get(),
2442ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                                  VS, HasInClassInit);
2443ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2444ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (VarTemplateDecl *VT =
24456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
2446ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        // Re-direct this decl to refer to the templated decl so that we can
2447ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        // initialize it.
2448ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        ThisDecl = VT->getTemplatedDecl();
2449ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2450fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer      if (ThisDecl && AccessAttrs)
24514a97b8e75f5dccf5a9537cf7358297437f55326dRichard Smith        Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
245237b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor    }
2453eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski
2454147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    // Handle the initializer.
24551d87fbaeea4a9fbbd73b3a53641f59f1673098e5David Blaikie    if (HasInClassInit != ICIS_NoInit &&
24561d87fbaeea4a9fbbd73b3a53641f59f1673098e5David Blaikie        DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
24571d87fbaeea4a9fbbd73b3a53641f59f1673098e5David Blaikie        DeclSpec::SCS_static) {
2458147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      // The initializer was deferred; parse it and cache the tokens.
2459fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer      Diag(Tok, getLangOpts().CPlusPlus11
2460fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer                    ? diag::warn_cxx98_compat_nonstatic_member_init
2461fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer                    : diag::ext_nonstatic_member_init);
24627fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith
24637a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (DeclaratorInfo.isArrayOfUnknownBound()) {
2464ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith        // C++11 [dcl.array]p3: An array bound may also be omitted when the
2465ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith        // declarator is followed by an initializer.
24667a614d8380297fcd2bc23986241905d97222948cRichard Smith        //
24677a614d8380297fcd2bc23986241905d97222948cRichard Smith        // A brace-or-equal-initializer for a member-declarator is not an
24683164c14cadbb09a05ba811602221e9156077cf44David Blaikie        // initializer in the grammar, so this is ill-formed.
24697a614d8380297fcd2bc23986241905d97222948cRichard Smith        Diag(Tok, diag::err_incomplete_array_member_init);
24708fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2471fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer
2472fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        // Avoid later warnings about a class member of incomplete type.
24733164c14cadbb09a05ba811602221e9156077cf44David Blaikie        if (ThisDecl)
24743164c14cadbb09a05ba811602221e9156077cf44David Blaikie          ThisDecl->setInvalidDecl();
24757a614d8380297fcd2bc23986241905d97222948cRichard Smith      } else
24767a614d8380297fcd2bc23986241905d97222948cRichard Smith        ParseCXXNonStaticMemberInitializer(ThisDecl);
2477147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    } else if (HasInitializer) {
2478147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      // Normal initializer.
2479a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor      if (!Init.isUsable())
2480fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        Init = ParseCXXMemberInitializer(
2481fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer            ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2482fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer
2483147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      if (Init.isInvalid())
24848fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2485147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      else if (ThisDecl)
248633deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl        Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
2487a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                                     DS.containsPlaceholderType());
2488fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer    } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
2489147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      // No initializer.
2490a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
2491fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer
2492147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    if (ThisDecl) {
2493fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer      if (!ThisDecl->isInvalidDecl()) {
2494fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        // Set the Decl for any late parsed attributes
2495fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2496fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer          CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2497fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer
2498fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2499fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer          LateParsedAttrs[i]->addDecl(ThisDecl);
2500fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer      }
2501147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      Actions.FinalizeDeclaration(ThisDecl);
2502147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor      DeclsInGroup.push_back(ThisDecl);
2503fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer
2504fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer      if (DeclaratorInfo.isFunctionDeclarator() &&
2505fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer          DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2506fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer              DeclSpec::SCS_typedef)
2507fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer        HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
2508147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    }
2509fcbe20811e3848869054ad13352cbb431bd423ebDavid Majnemer    LateParsedAttrs.clear();
2510147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor
2511147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor    DeclaratorInfo.complete(ThisDecl);
25127a614d8380297fcd2bc23986241905d97222948cRichard Smith
25134cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // If we don't have a comma, it is either the end of the list (a ';')
25144cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // or an error, bail out.
2515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SourceLocation CommaLoc;
2516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!TryConsumeToken(tok::comma, CommaLoc))
25174cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis      break;
25181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25191c94c16317c1a35c1549e022958188eea2567089Richard Smith    if (Tok.isAtStartOfLine() &&
25201c94c16317c1a35c1549e022958188eea2567089Richard Smith        !MightBeDeclarator(Declarator::MemberContext)) {
25211c94c16317c1a35c1549e022958188eea2567089Richard Smith      // This comma was followed by a line-break and something which can't be
25221c94c16317c1a35c1549e022958188eea2567089Richard Smith      // the start of a declarator. The comma was probably a typo for a
25231c94c16317c1a35c1549e022958188eea2567089Richard Smith      // semicolon.
25241c94c16317c1a35c1549e022958188eea2567089Richard Smith      Diag(CommaLoc, diag::err_expected_semi_declaration)
25251c94c16317c1a35c1549e022958188eea2567089Richard Smith        << FixItHint::CreateReplacement(CommaLoc, ";");
25261c94c16317c1a35c1549e022958188eea2567089Richard Smith      ExpectSemi = false;
25271c94c16317c1a35c1549e022958188eea2567089Richard Smith      break;
25281c94c16317c1a35c1549e022958188eea2567089Richard Smith    }
25291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25304cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // Parse the next declarator.
25314cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    DeclaratorInfo.clear();
25324867347e82648d3baf09524b98b09c297a5a198fNico Weber    VS.clear();
25330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    BitfieldSize = ExprResult(/*Invalid=*/false);
25340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Init = ExprResult(/*Invalid=*/false);
2535a2b4e5d9292688bc67b583592918dbeecae31ea3Douglas Gregor    HasInitializer = false;
25367984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith    DeclaratorInfo.setCommaLoc(CommaLoc);
25371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2538651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // GNU attributes are allowed before the second and subsequent declarator.
25397f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    MaybeParseGNUAttributes(DeclaratorInfo);
25404cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
25410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (ParseCXXMemberDeclaratorBeforeInitializer(
25420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
25430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      break;
25444cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
25454cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
25461c94c16317c1a35c1549e022958188eea2567089Richard Smith  if (ExpectSemi &&
25471c94c16317c1a35c1549e022958188eea2567089Richard Smith      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
2548ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner    // Skip to end of block or statement.
25498fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2550ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner    // If we stopped at a ';', eat it.
2551651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    TryConsumeToken(tok::semi);
2552682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return;
25534cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
25544cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
25554549d7ffc15bdd7ab860aa68db089d9f559b79e7Rafael Espindola  Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
25564cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
25574cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
25587a614d8380297fcd2bc23986241905d97222948cRichard Smith/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
25597a614d8380297fcd2bc23986241905d97222948cRichard Smith/// pure-specifier. Also detect and reject any attempted defaulted/deleted
25607a614d8380297fcd2bc23986241905d97222948cRichard Smith/// function definition. The location of the '=', if any, will be placed in
25617a614d8380297fcd2bc23986241905d97222948cRichard Smith/// EqualLoc.
25627a614d8380297fcd2bc23986241905d97222948cRichard Smith///
25637a614d8380297fcd2bc23986241905d97222948cRichard Smith///   pure-specifier:
25647a614d8380297fcd2bc23986241905d97222948cRichard Smith///     '= 0'
256533deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl///
25667a614d8380297fcd2bc23986241905d97222948cRichard Smith///   brace-or-equal-initializer:
25677a614d8380297fcd2bc23986241905d97222948cRichard Smith///     '=' initializer-expression
256833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl///     braced-init-list
256933deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl///
25707a614d8380297fcd2bc23986241905d97222948cRichard Smith///   initializer-clause:
25717a614d8380297fcd2bc23986241905d97222948cRichard Smith///     assignment-expression
257233deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl///     braced-init-list
257333deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl///
2574b310439121c875937d78cc49cc969bc1197fc025Richard Smith///   defaulted/deleted function-definition:
25757a614d8380297fcd2bc23986241905d97222948cRichard Smith///     '=' 'default'
25767a614d8380297fcd2bc23986241905d97222948cRichard Smith///     '=' 'delete'
25777a614d8380297fcd2bc23986241905d97222948cRichard Smith///
25787a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Prior to C++0x, the assignment-expression in an initializer-clause must
25797a614d8380297fcd2bc23986241905d97222948cRichard Smith/// be a constant-expression.
2580552e29985a710f4ced62b39d70557501bd31ca9bDouglas GregorExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
25817a614d8380297fcd2bc23986241905d97222948cRichard Smith                                             SourceLocation &EqualLoc) {
25827a614d8380297fcd2bc23986241905d97222948cRichard Smith  assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
25837a614d8380297fcd2bc23986241905d97222948cRichard Smith         && "Data member initializer not starting with '=' or '{'");
25847a614d8380297fcd2bc23986241905d97222948cRichard Smith
2585552e29985a710f4ced62b39d70557501bd31ca9bDouglas Gregor  EnterExpressionEvaluationContext Context(Actions,
2586552e29985a710f4ced62b39d70557501bd31ca9bDouglas Gregor                                           Sema::PotentiallyEvaluated,
2587552e29985a710f4ced62b39d70557501bd31ca9bDouglas Gregor                                           D);
2588651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TryConsumeToken(tok::equal, EqualLoc)) {
25897a614d8380297fcd2bc23986241905d97222948cRichard Smith    if (Tok.is(tok::kw_delete)) {
25907a614d8380297fcd2bc23986241905d97222948cRichard Smith      // In principle, an initializer of '= delete p;' is legal, but it will
25917a614d8380297fcd2bc23986241905d97222948cRichard Smith      // never type-check. It's better to diagnose it as an ill-formed expression
25927a614d8380297fcd2bc23986241905d97222948cRichard Smith      // than as an ill-formed deleted non-function member.
25937a614d8380297fcd2bc23986241905d97222948cRichard Smith      // An initializer of '= delete p, foo' will never be parsed, because
25947a614d8380297fcd2bc23986241905d97222948cRichard Smith      // a top-level comma always ends the initializer expression.
25957a614d8380297fcd2bc23986241905d97222948cRichard Smith      const Token &Next = NextToken();
25967a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2597651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          Next.is(tok::eof)) {
25987a614d8380297fcd2bc23986241905d97222948cRichard Smith        if (IsFunction)
25997a614d8380297fcd2bc23986241905d97222948cRichard Smith          Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
26007a614d8380297fcd2bc23986241905d97222948cRichard Smith            << 1 /* delete */;
26017a614d8380297fcd2bc23986241905d97222948cRichard Smith        else
26027a614d8380297fcd2bc23986241905d97222948cRichard Smith          Diag(ConsumeToken(), diag::err_deleted_non_function);
2603c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        return ExprError();
26047a614d8380297fcd2bc23986241905d97222948cRichard Smith      }
26057a614d8380297fcd2bc23986241905d97222948cRichard Smith    } else if (Tok.is(tok::kw_default)) {
26067a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (IsFunction)
26077a614d8380297fcd2bc23986241905d97222948cRichard Smith        Diag(Tok, diag::err_default_delete_in_multiple_declaration)
26087a614d8380297fcd2bc23986241905d97222948cRichard Smith          << 0 /* default */;
26097a614d8380297fcd2bc23986241905d97222948cRichard Smith      else
26107a614d8380297fcd2bc23986241905d97222948cRichard Smith        Diag(ConsumeToken(), diag::err_default_special_members);
2611c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      return ExprError();
26127a614d8380297fcd2bc23986241905d97222948cRichard Smith    }
26130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
26140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
26150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Diag(Tok, diag::err_ms_property_initializer) << PD;
26160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return ExprError();
261733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl  }
261833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl  return ParseInitializer();
26197a614d8380297fcd2bc23986241905d97222948cRichard Smith}
26207a614d8380297fcd2bc23986241905d97222948cRichard Smith
26214cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXMemberSpecification - Parse the class definition.
26224cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
26234cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///       member-specification:
26244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         member-declaration member-specification[opt]
26254cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///         access-specifier ':' member-specification[opt]
26264cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
262717d35c36fbae764fcd68fa8b31624078a033aabcJoao Matosvoid Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
262807fc1ba7553f2f5bf26984091197311decd9028eMichael Han                                         SourceLocation AttrFixitLoc,
2629053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith                                         ParsedAttributesWithRange &Attrs,
263017d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos                                         unsigned TagType, Decl *TagDecl) {
263117d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos  assert((TagType == DeclSpec::TST_struct ||
263217d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos         TagType == DeclSpec::TST_interface ||
263317d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos         TagType == DeclSpec::TST_union  ||
263417d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos         TagType == DeclSpec::TST_class) && "Invalid TagType!");
263517d35c36fbae764fcd68fa8b31624078a033aabcJoao Matos
2636f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2637f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing struct/union/class body");
26381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
263926997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor  // Determine whether this is a non-nested class. Note that local
264026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor  // classes are *not* considered to be nested classes.
264126997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor  bool NonNestedClass = true;
264226997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor  if (!ClassStack.empty()) {
264323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    for (const Scope *S = getCurScope(); S; S = S->getParent()) {
264426997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor      if (S->isClassScope()) {
264526997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        // We're inside a class scope, so this is a nested class.
264626997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        NonNestedClass = false;
2647e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall
2648e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        // The Microsoft extension __interface does not permit nested classes.
2649e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        if (getCurrentClass().IsInterface) {
2650e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall          Diag(RecordLoc, diag::err_invalid_member_in_interface)
2651e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall            << /*ErrorType=*/6
2652e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall            << (isa<NamedDecl>(TagDecl)
2653e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall                  ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2654651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                  : "(anonymous)");
2655e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        }
265626997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        break;
265726997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor      }
265826997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor
265926997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor      if ((S->getFlags() & Scope::FnScope)) {
266026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        // If we're in a function or function template declared in the
266126997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        // body of a class, then this is a local class rather than a
266226997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        // nested class.
266326997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        const Scope *Parent = S->getParent();
266426997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        if (Parent->isTemplateParamScope())
266526997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor          Parent = Parent->getParent();
266626997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor        if (Parent->isClassScope())
266726997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor          break;
266826997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor      }
266926997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor    }
267026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor  }
26714cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
26724cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // Enter a scope for the class.
26733218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor  ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
26744cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
26756569d68745c8213709740337d2be52b031384f58Douglas Gregor  // Note that we are parsing a new (potentially-nested) class definition.
2676e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall  ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2677e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall                                    TagType == DeclSpec::TST_interface);
26786569d68745c8213709740337d2be52b031384f58Douglas Gregor
2679ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  if (TagDecl)
268023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
2681bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall
2682b184a18a0a52e4bec33ef70f13bfcc29aafa14f2Anders Carlsson  SourceLocation FinalLoc;
26837121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer  bool IsFinalSpelledSealed = false;
2684b184a18a0a52e4bec33ef70f13bfcc29aafa14f2Anders Carlsson
2685b184a18a0a52e4bec33ef70f13bfcc29aafa14f2Anders Carlsson  // Parse the optional 'final' keyword.
26864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
26877121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
26887121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    assert((Specifier == VirtSpecifiers::VS_Final ||
26897121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer            Specifier == VirtSpecifiers::VS_Sealed) &&
26907121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer           "not a class definition");
26918b11b5e6ce086fcaa7344bf84cffefcf4b53f9f6Richard Smith    FinalLoc = ConsumeToken();
26927121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
2693b184a18a0a52e4bec33ef70f13bfcc29aafa14f2Anders Carlsson
26947121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    if (TagType == DeclSpec::TST_interface)
2695e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall      Diag(FinalLoc, diag::err_override_control_interface)
26967121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer        << VirtSpecifiers::getSpecifierName(Specifier);
26977121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    else if (Specifier == VirtSpecifiers::VS_Final)
26987121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer      Diag(FinalLoc, getLangOpts().CPlusPlus11
26997121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer                         ? diag::warn_cxx98_compat_override_control_keyword
27007121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer                         : diag::ext_override_control_keyword)
27017121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer        << VirtSpecifiers::getSpecifierName(Specifier);
27027121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer    else if (Specifier == VirtSpecifiers::VS_Sealed)
27037121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer      Diag(FinalLoc, diag::ext_ms_sealed_keyword);
27042e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han
270507fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // Parse any C++11 attributes after 'final' keyword.
270607fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // These attributes are not allowed to appear here,
270707fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // and the only possible place for them to appertain
270807fc1ba7553f2f5bf26984091197311decd9028eMichael Han    // to the class would be between class-key and class-name.
2709053214013990ad8ec096dafc64aa7c0ad2b05bc0Richard Smith    CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
27100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
27110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // ParseClassSpecifier() does only a superficial check for attributes before
27120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // deciding to call this method.  For example, for
27130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // `class C final alignas ([l) {` it will decide that this looks like a
27140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // misplaced attribute since it sees `alignas '(' ')'`.  But the actual
27150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // attribute parsing code will try to parse the '[' as a constexpr lambda
27160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // and consume enough tokens that the alignas parsing code will eat the
27170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // opening '{'.  So bail out if the next token isn't one we expect.
27180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
27190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (TagDecl)
27200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
27210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return;
27220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
2723b184a18a0a52e4bec33ef70f13bfcc29aafa14f2Anders Carlsson  }
2724cc54d594d4f6509c0e3a8e349e481d9b5d899df6Anders Carlsson
2725bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall  if (Tok.is(tok::colon)) {
2726bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall    ParseBaseClause(TagDecl);
2727bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall    if (!Tok.is(tok::l_brace)) {
2728176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      bool SuggestFixIt = false;
2729176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2730176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (Tok.isAtStartOfLine()) {
2731176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        switch (Tok.getKind()) {
2732176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::kw_private:
2733176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::kw_protected:
2734176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::kw_public:
2735176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          SuggestFixIt = NextToken().getKind() == tok::colon;
2736176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          break;
2737176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::kw_static_assert:
2738176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::r_brace:
2739176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::kw_using:
2740176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        // base-clause can have simple-template-id; 'template' can't be there
2741176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::kw_template:
2742176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          SuggestFixIt = true;
2743176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          break;
2744176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case tok::identifier:
2745176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          SuggestFixIt = isConstructorDeclarator(true);
2746176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          break;
2747176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        default:
2748176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2749176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          break;
2750176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        }
2751176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
2752176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      DiagnosticBuilder LBraceDiag =
2753176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2754176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (SuggestFixIt) {
2755176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2756176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        // Try recovering from missing { after base-clause.
2757176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        PP.EnterToken(Tok);
2758176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        Tok.setKind(tok::l_brace);
2759176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      } else {
2760176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        if (TagDecl)
2761176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2762176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return;
2763176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
2764bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall    }
2765bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall  }
2766bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall
2767bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall  assert(Tok.is(tok::l_brace));
27684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
27694a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
2770bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall
277142a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall  if (TagDecl)
27722c3ee54e51d835a35bbf781c69e17f39e2ba0480Anders Carlsson    Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
27737121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer                                            IsFinalSpelledSealed,
27744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getOpenLocation());
2775f9368159334ff86ea5fa367225c1a580977f3b03John McCall
27764cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // C++ 11p3: Members of a class defined with the keyword class are private
27774cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // by default. Members of a class defined with the keywords struct or union
27784cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // are public by default.
27794cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  AccessSpecifier CurAS;
27804cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  if (TagType == DeclSpec::TST_class)
27814cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    CurAS = AS_private;
27824cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  else
27834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    CurAS = AS_public;
27845f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen  ParsedAttributes AccessAttrs(AttrFactory);
27854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
278607976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor  if (TagDecl) {
278707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor    // While we still have something to read, read the member-declarations.
2788651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
278907976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      // Each iteration of this loop reads one member-declaration.
279007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor
27914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
2792563a645de82231a55e221fe655b7188bf8369662Francois Pichet          Tok.is(tok::kw___if_not_exists))) {
2793563a645de82231a55e221fe655b7188bf8369662Francois Pichet        ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2794563a645de82231a55e221fe655b7188bf8369662Francois Pichet        continue;
2795563a645de82231a55e221fe655b7188bf8369662Francois Pichet      }
2796563a645de82231a55e221fe655b7188bf8369662Francois Pichet
279707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      // Check for extraneous top-level semicolon.
279807976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      if (Tok.is(tok::semi)) {
2799eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        ConsumeExtraSemi(InsideStruct, TagType);
280007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor        continue;
280107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      }
28021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2803aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman      if (Tok.is(tok::annot_pragma_vis)) {
2804aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman        HandlePragmaVisibility();
2805aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman        continue;
2806aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman      }
2807aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman
2808aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman      if (Tok.is(tok::annot_pragma_pack)) {
2809aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman        HandlePragmaPack();
2810aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman        continue;
2811aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman      }
2812aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman
2813f4deaef8b816bd43258c30fe8e657ab041b675adArgyrios Kyrtzidis      if (Tok.is(tok::annot_pragma_align)) {
2814f4deaef8b816bd43258c30fe8e657ab041b675adArgyrios Kyrtzidis        HandlePragmaAlign();
2815f4deaef8b816bd43258c30fe8e657ab041b675adArgyrios Kyrtzidis        continue;
2816f4deaef8b816bd43258c30fe8e657ab041b675adArgyrios Kyrtzidis      }
2817f4deaef8b816bd43258c30fe8e657ab041b675adArgyrios Kyrtzidis
2818c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev      if (Tok.is(tok::annot_pragma_openmp)) {
2819c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev        ParseOpenMPDeclarativeDirective();
2820c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev        continue;
2821c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev      }
2822c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2823651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2824651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        HandlePragmaMSPointersToMembers();
2825651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        continue;
2826651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      }
2827651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
28286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (Tok.is(tok::annot_pragma_ms_pragma)) {
28296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        HandlePragmaMSPragma();
28306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        continue;
28316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
28326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
2833b310439121c875937d78cc49cc969bc1197fc025Richard Smith      // If we see a namespace here, a close brace was missing somewhere.
2834b310439121c875937d78cc49cc969bc1197fc025Richard Smith      if (Tok.is(tok::kw_namespace)) {
28357faf81ff1dc8f8dc724e928ba90ccbfad0fdc2bcRichard Smith        DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
2836b310439121c875937d78cc49cc969bc1197fc025Richard Smith        break;
2837b310439121c875937d78cc49cc969bc1197fc025Richard Smith      }
2838b310439121c875937d78cc49cc969bc1197fc025Richard Smith
283907976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      AccessSpecifier AS = getAccessSpecifierIfPresent();
284007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      if (AS != AS_none) {
284107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor        // Current token is a C++ access specifier.
284207976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor        CurAS = AS;
284307976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor        SourceLocation ASLoc = Tok.getLocation();
284413f8daf70637f8f295134ac8e089dd7721e09085David Blaikie        unsigned TokLength = Tok.getLength();
284507976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor        ConsumeToken();
28465f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen        AccessAttrs.clear();
28475f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen        MaybeParseGNUAttributes(AccessAttrs);
28485f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen
284913f8daf70637f8f295134ac8e089dd7721e09085David Blaikie        SourceLocation EndLoc;
2850651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (TryConsumeToken(tok::colon, EndLoc)) {
2851651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        } else if (TryConsumeToken(tok::semi, EndLoc)) {
2852651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          Diag(EndLoc, diag::err_expected)
2853651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
285413f8daf70637f8f295134ac8e089dd7721e09085David Blaikie        } else {
285513f8daf70637f8f295134ac8e089dd7721e09085David Blaikie          EndLoc = ASLoc.getLocWithOffset(TokLength);
2856651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          Diag(EndLoc, diag::err_expected)
2857651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
285813f8daf70637f8f295134ac8e089dd7721e09085David Blaikie        }
2859c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen
2860e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        // The Microsoft extension __interface does not permit non-public
2861e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        // access specifiers.
2862e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2863e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall          Diag(ASLoc, diag::err_access_specifier_interface)
2864e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall            << (CurAS == AS_protected);
2865e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall        }
2866e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall
2867c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen        if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2868c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen                                         AccessAttrs.getList())) {
2869c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen          // found another attribute than only annotations
2870c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen          AccessAttrs.clear();
2871c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen        }
2872c35cba4a54106117a52b267c7040b3bea9a4d18eErik Verbruggen
287307976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor        continue;
287407976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      }
28754cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
287607976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor      // Parse all the comma separated declarators.
28775f1c822def3efffe1d8f7299fbbbc3b1cdd4833dErik Verbruggen      ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
287807976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor    }
28791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
288107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor  } else {
28828fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace);
28834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
28841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // If attributes exist after class contents, parse them.
28860b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributes attrs(AttrFactory);
28877f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(attrs);
28884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
288942a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall  if (TagDecl)
289023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
28914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                              T.getOpenLocation(),
28924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                              T.getCloseLocation(),
28937f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              attrs.getList());
28944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
289574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor  // C++11 [class.mem]p2:
289674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor  //   Within the class member-specification, the class is regarded as complete
2897176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  //   within function bodies, default arguments, exception-specifications, and
289874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor  //   brace-or-equal-initializers for non-static data members (including such
289974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor  //   things in nested classes).
290007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor  if (TagDecl && NonNestedClass) {
29014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis    // We are not inside a nested class. This class and its nested classes
290272b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor    // are complete and we can parse the delayed portions of method
2903eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski    // declarations and the lexed inline method definitions, along with any
2904eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski    // delayed attributes.
2905e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor    SourceLocation SavedPrevTokLocation = PrevTokLocation;
2906eff98fc3561f6b717f6348f04b3f4fe03e934466Caitlin Sadowski    ParseLexedAttributes(getCurrentClass());
29076569d68745c8213709740337d2be52b031384f58Douglas Gregor    ParseLexedMethodDeclarations(getCurrentClass());
2908a4156b8574666aa69a2b0ad35dc9e9603433e4aeRichard Smith
2909a4156b8574666aa69a2b0ad35dc9e9603433e4aeRichard Smith    // We've finished with all pending member declarations.
2910a4156b8574666aa69a2b0ad35dc9e9603433e4aeRichard Smith    Actions.ActOnFinishCXXMemberDecls();
2911a4156b8574666aa69a2b0ad35dc9e9603433e4aeRichard Smith
29127a614d8380297fcd2bc23986241905d97222948cRichard Smith    ParseLexedMemberInitializers(getCurrentClass());
29136569d68745c8213709740337d2be52b031384f58Douglas Gregor    ParseLexedMethodDefs(getCurrentClass());
2914e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor    PrevTokLocation = SavedPrevTokLocation;
29154cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  }
29164cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
291742a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall  if (TagDecl)
29184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
29194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                     T.getCloseLocation());
2920db7bb4a4e7d9744cbc994c90932e6f056228e1ffJohn McCall
29214cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  // Leave the class scope.
29226569d68745c8213709740337d2be52b031384f58Douglas Gregor  ParsingDef.Pop();
29238935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ClassScope.Exit();
29244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
29257ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
29267faf81ff1dc8f8dc724e928ba90ccbfad0fdc2bcRichard Smithvoid Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
2927b310439121c875937d78cc49cc969bc1197fc025Richard Smith  assert(Tok.is(tok::kw_namespace));
2928b310439121c875937d78cc49cc969bc1197fc025Richard Smith
2929b310439121c875937d78cc49cc969bc1197fc025Richard Smith  // FIXME: Suggest where the close brace should have gone by looking
2930b310439121c875937d78cc49cc969bc1197fc025Richard Smith  // at indentation changes within the definition body.
29317faf81ff1dc8f8dc724e928ba90ccbfad0fdc2bcRichard Smith  Diag(D->getLocation(),
29327faf81ff1dc8f8dc724e928ba90ccbfad0fdc2bcRichard Smith       diag::err_missing_end_of_definition) << D;
2933b310439121c875937d78cc49cc969bc1197fc025Richard Smith  Diag(Tok.getLocation(),
29347faf81ff1dc8f8dc724e928ba90ccbfad0fdc2bcRichard Smith       diag::note_missing_end_of_definition_before) << D;
2935b310439121c875937d78cc49cc969bc1197fc025Richard Smith
2936b310439121c875937d78cc49cc969bc1197fc025Richard Smith  // Push '};' onto the token stream to recover.
2937b310439121c875937d78cc49cc969bc1197fc025Richard Smith  PP.EnterToken(Tok);
2938b310439121c875937d78cc49cc969bc1197fc025Richard Smith
2939b310439121c875937d78cc49cc969bc1197fc025Richard Smith  Tok.startToken();
2940b310439121c875937d78cc49cc969bc1197fc025Richard Smith  Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2941b310439121c875937d78cc49cc969bc1197fc025Richard Smith  Tok.setKind(tok::semi);
2942b310439121c875937d78cc49cc969bc1197fc025Richard Smith  PP.EnterToken(Tok);
2943b310439121c875937d78cc49cc969bc1197fc025Richard Smith
2944b310439121c875937d78cc49cc969bc1197fc025Richard Smith  Tok.setKind(tok::r_brace);
2945b310439121c875937d78cc49cc969bc1197fc025Richard Smith}
2946b310439121c875937d78cc49cc969bc1197fc025Richard Smith
29477ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// ParseConstructorInitializer - Parse a C++ constructor initializer,
29487ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// which explicitly initializes the members or base classes of a
29497ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// class (C++ [class.base.init]). For example, the three initializers
29507ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// after the ':' in the Derived constructor below:
29517ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///
29527ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// @code
29537ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// class Base { };
29547ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// class Derived : Base {
29557ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///   int x;
29567ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///   float f;
29577ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// public:
29587ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///   Derived(float f) : Base(), x(17), f(f) { }
29597ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// };
29607ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// @endcode
29617ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///
29621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [C++]  ctor-initializer:
29631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///          ':' mem-initializer-list
29647ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///
29651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [C++]  mem-initializer-list:
29663fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor///          mem-initializer ...[opt]
29673fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor///          mem-initializer ...[opt] , mem-initializer-list
2968d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
29697ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
29707ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
297128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
297228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
29737ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  SourceLocation ColonLoc = ConsumeToken();
29741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<CXXCtorInitializer*, 4> MemInitializers;
29769db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
2977193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
29787ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  do {
29790133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor    if (Tok.is(tok::code_completion)) {
2980572cf585da655522651b589101784c58902f8690Dmitri Gribenko      Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2981572cf585da655522651b589101784c58902f8690Dmitri Gribenko                                                 MemInitializers);
29827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
29830133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor    } else {
29840133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor      MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
29850133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor      if (!MemInit.isInvalid())
29860133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor        MemInitializers.push_back(MemInit.get());
29870133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor      else
29880133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor        AnyErrors = true;
29890133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor    }
29900133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor
29917ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    if (Tok.is(tok::comma))
29927ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor      ConsumeToken();
29937ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    else if (Tok.is(tok::l_brace))
29947ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor      break;
2995b1f6fa48960eae269a3931d1fc545ed468d9a4d2Douglas Gregor    // If the next token looks like a base or member initializer, assume that
2996b1f6fa48960eae269a3931d1fc545ed468d9a4d2Douglas Gregor    // we're just missing a comma.
2997751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor    else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2998751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor      SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2999751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor      Diag(Loc, diag::err_ctor_init_missing_comma)
3000751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor        << FixItHint::CreateInsertion(Loc, ", ");
3001751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor    } else {
30027ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor      // Skip over garbage, until we get to '{'.  Don't eat the '{'.
3003651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3004651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                         << tok::comma;
30058fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
30067ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor      break;
30077ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    }
30087ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  } while (true);
30097ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
301093c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie  Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
30119db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                               AnyErrors);
30127ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor}
30137ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
30147ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// ParseMemInitializer - Parse a C++ member initializer, which is
30157ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// part of a constructor initializer that explicitly initializes one
30167ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// member or base class (C++ [class.base.init]). See
30177ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// ParseConstructorInitializer for an example.
30187ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///
30197ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] mem-initializer:
30207ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///         mem-initializer-id '(' expression-list[opt] ')'
3021dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] mem-initializer-id braced-init-list
30221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
30237ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] mem-initializer-id:
30247ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///         '::'[opt] nested-name-specifier[opt] class-name
30257ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor///         identifier
3026176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesMemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
3027bcfad54a43e5570e09daddd976bd4545933e75b1Fariborz Jahanian  // parse '::'[opt] nested-name-specifier[opt]
3028bcfad54a43e5570e09daddd976bd4545933e75b1Fariborz Jahanian  CXXScopeSpec SS;
3029efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
3030b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TemplateTypeTy;
3031961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian  if (Tok.is(tok::annot_template_id)) {
303225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
3033d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor    if (TemplateId->Kind == TNK_Type_template ||
3034d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor        TemplateId->Kind == TNK_Dependent_template_name) {
3035059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
3036961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian      assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
3037b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      TemplateTypeTy = getTypeAnnotation(Tok);
3038961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian    }
3039961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian  }
3040f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  // Uses of decltype will already have been converted to annot_decltype by
3041f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  // ParseOptionalCXXScopeSpecifier at this point.
3042f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3043f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie      && Tok.isNot(tok::annot_decltype)) {
30441ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_member_or_base_name);
30457ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    return true;
30467ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  }
30471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *II = nullptr;
3049f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  DeclSpec DS(AttrFactory);
3050f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  SourceLocation IdLoc = Tok.getLocation();
3051f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  if (Tok.is(tok::annot_decltype)) {
3052f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie    // Get the decltype expression, if there is one.
3053f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie    ParseDecltypeSpecifier(DS);
3054f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  } else {
3055f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie    if (Tok.is(tok::identifier))
3056f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie      // Get the identifier. This may be a member name or a class name,
3057f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie      // but we'll let the semantic analysis determine which it is.
3058f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie      II = Tok.getIdentifierInfo();
3059f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie    ConsumeToken();
3060f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie  }
3061f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie
30627ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
30637ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // Parse the '('.
306480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
30657fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
30667fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith
30676df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    ExprResult InitList = ParseBraceInitializer();
30686df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    if (InitList.isInvalid())
30696df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      return true;
30706df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
30716df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    SourceLocation EllipsisLoc;
3072651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    TryConsumeToken(tok::ellipsis, EllipsisLoc);
30736df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
30746df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
3075f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie                                       TemplateTypeTy, DS, IdLoc,
3076c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                       InitList.get(), EllipsisLoc);
3077dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else if(Tok.is(tok::l_paren)) {
30784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
30794a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
3080dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
3081dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // Parse the optional expression-list.
30824e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector ArgExprs;
3083dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    CommaLocsTy CommaLocs;
3084dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
30858fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
3086dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      return true;
3087dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    }
30887ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
30894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
30907ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
3091dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    SourceLocation EllipsisLoc;
3092651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    TryConsumeToken(tok::ellipsis, EllipsisLoc);
30937ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
3094dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
3095f211662199c87461f3b1475a549ab439c63ca83bDavid Blaikie                                       TemplateTypeTy, DS, IdLoc,
3096a36bbac10f7b74ef198ec2fb0eb52dbd8a50e7f0Dmitri Gribenko                                       T.getOpenLocation(), ArgExprs,
3097a36bbac10f7b74ef198ec2fb0eb52dbd8a50e7f0Dmitri Gribenko                                       T.getCloseLocation(), EllipsisLoc);
3098dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  }
3099dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
3100651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (getLangOpts().CPlusPlus11)
3101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  else
3103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return Diag(Tok, diag::err_expected) << tok::l_paren;
31047ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor}
31050fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor
31067acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
31070fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor///
3108a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor///       exception-specification:
31097acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///         dynamic-exception-specification
31107acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///         noexcept-specification
31117acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///
31127acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///       noexcept-specification:
31137acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///         'noexcept'
31147acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///         'noexcept' '(' constant-expression ')'
31157acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian RedlExceptionSpecificationType
3116176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesParser::tryParseExceptionSpecification(bool Delayed,
311774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                    SourceRange &SpecificationRange,
31185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                    SmallVectorImpl<ParsedType> &DynamicExceptions,
31195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                    SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
3120176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                    ExprResult &NoexceptExpr,
3121176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                    CachedTokens *&ExceptionSpecTokens) {
31227acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  ExceptionSpecificationType Result = EST_None;
3123176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ExceptionSpecTokens = 0;
3124176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
3125176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // Handle delayed parsing of exception-specifications.
3126176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (Delayed) {
3127176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3128176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      return EST_None;
3129176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
3130176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Consume and cache the starting token.
3131176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    bool IsNoexcept = Tok.is(tok::kw_noexcept);
3132176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Token StartTok = Tok;
3133176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SpecificationRange = SourceRange(ConsumeToken());
3134176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
3135176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Check for a '('.
3136176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (!Tok.is(tok::l_paren)) {
3137176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // If this is a bare 'noexcept', we're done.
3138176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (IsNoexcept) {
3139176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3140176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        NoexceptExpr = 0;
3141176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return EST_BasicNoexcept;
3142176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
3143176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
3144176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Diag(Tok, diag::err_expected_lparen_after) << "throw";
3145176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      return EST_DynamicNone;
3146176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
3147176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
3148176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // Cache the tokens for the exception-specification.
3149176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExceptionSpecTokens = new CachedTokens;
3150176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3151176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExceptionSpecTokens->push_back(Tok); // '('
3152176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SpecificationRange.setEnd(ConsumeParen()); // '('
31530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
31540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
31550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         /*StopAtSemi=*/true,
31560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         /*ConsumeFinalToken=*/true);
3157176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SpecificationRange.setEnd(Tok.getLocation());
3158176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return EST_Unparsed;
3159176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
3160176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
31617acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  // See if there's a dynamic specification.
31627acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  if (Tok.is(tok::kw_throw)) {
31637acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    Result = ParseDynamicExceptionSpecification(SpecificationRange,
31647acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl                                                DynamicExceptions,
31657acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl                                                DynamicExceptionRanges);
31667acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
31677acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl           "Produced different number of exception types and ranges.");
31687acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  }
31697acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
31707acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  // If there's no noexcept specification, we're done.
31717acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  if (Tok.isNot(tok::kw_noexcept))
31727acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    return Result;
31737acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
3174841804baff6ea8ba1904a2ba81265aae1479e882Richard Smith  Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3175841804baff6ea8ba1904a2ba81265aae1479e882Richard Smith
31767acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  // If we already had a dynamic specification, parse the noexcept for,
31777acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  // recovery, but emit a diagnostic and don't store the results.
31787acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  SourceRange NoexceptRange;
31797acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  ExceptionSpecificationType NoexceptType = EST_None;
31807acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
31817acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  SourceLocation KeywordLoc = ConsumeToken();
31827acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  if (Tok.is(tok::l_paren)) {
31837acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    // There is an argument.
31844a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
31854a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
31867acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    NoexceptType = EST_ComputedNoexcept;
31877acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    NoexceptExpr = ParseConstantExpression();
318860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    // The argument must be contextually convertible to bool. We use
318960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    // ActOnBooleanCondition for this purpose.
319060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    if (!NoexceptExpr.isInvalid())
319160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
319260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                                   NoexceptExpr.get());
31934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
31944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
31957acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  } else {
31967acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    // There is no argument.
31977acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    NoexceptType = EST_BasicNoexcept;
31987acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
31997acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  }
32007acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
32017acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  if (Result == EST_None) {
32027acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    SpecificationRange = NoexceptRange;
32037acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    Result = NoexceptType;
32047acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
32057acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    // If there's a dynamic specification after a noexcept specification,
32067acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    // parse that and ignore the results.
32077acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    if (Tok.is(tok::kw_throw)) {
32087acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl      Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
32097acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl      ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
32107acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl                                         DynamicExceptionRanges);
32117acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    }
32127acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  } else {
32137acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
32147acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  }
32157acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
32167acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  return Result;
32177acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl}
32187acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
321979f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smithstatic void diagnoseDynamicExceptionSpecification(
322079f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith    Parser &P, const SourceRange &Range, bool IsNoexcept) {
322179f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith  if (P.getLangOpts().CPlusPlus11) {
322279f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith    const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
322379f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith    P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
322479f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith    P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
322579f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith      << Replacement << FixItHint::CreateReplacement(Range, Replacement);
322679f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith  }
322779f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith}
322879f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith
32297acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl/// ParseDynamicExceptionSpecification - Parse a C++
32307acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl/// dynamic-exception-specification (C++ [except.spec]).
32317acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///
32327acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl///       dynamic-exception-specification:
3233a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor///         'throw' '(' type-id-list [opt] ')'
3234a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// [MS]    'throw' '(' '...' ')'
32351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
3236a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor///       type-id-list:
3237a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor///         type-id ... [opt]
3238a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor///         type-id-list ',' type-id ... [opt]
32390fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor///
32407acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian RedlExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
32417acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl                                  SourceRange &SpecificationRange,
32425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<ParsedType> &Exceptions,
32435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<SourceRange> &Ranges) {
32440fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor  assert(Tok.is(tok::kw_throw) && "expected throw");
32451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32467acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  SpecificationRange.setBegin(ConsumeToken());
32474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
32484a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
32497acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    Diag(Tok, diag::err_expected_lparen_after) << "throw";
32507acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    SpecificationRange.setEnd(SpecificationRange.getBegin());
325160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return EST_DynamicNone;
32520fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor  }
32530fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor
3254a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor  // Parse throw(...), a Microsoft extension that means "this function
3255a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor  // can throw anything".
3256a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor  if (Tok.is(tok::ellipsis)) {
3257a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor    SourceLocation EllipsisLoc = ConsumeToken();
32584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().MicrosoftExt)
3259a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor      Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
32604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
32614a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    SpecificationRange.setEnd(T.getCloseLocation());
326279f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith    diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
326360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return EST_MSAny;
3264a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor  }
3265a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor
32660fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor  // Parse the sequence of type-ids.
3267ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  SourceRange Range;
32680fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor  while (Tok.isNot(tok::r_paren)) {
3269ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    TypeResult Res(ParseTypeName(&Range));
32707acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
3271a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor    if (Tok.is(tok::ellipsis)) {
3272a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor      // C++0x [temp.variadic]p5:
3273a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor      //   - In a dynamic-exception-specification (15.4); the pattern is a
3274a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor      //     type-id.
3275a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor      SourceLocation Ellipsis = ConsumeToken();
32767acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl      Range.setEnd(Ellipsis);
3277a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor      if (!Res.isInvalid())
3278a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor        Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3279a04426c5416f22df1078f6b31c1619de73c40b59Douglas Gregor    }
32807acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
3281ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    if (!Res.isInvalid()) {
32827dc813462dd9fd3f6f4296f896a12de14264fef8Sebastian Redl      Exceptions.push_back(Res.get());
3283ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl      Ranges.push_back(Range);
3284ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    }
3285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3286651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!TryConsumeToken(tok::comma))
32870fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor      break;
32880fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor  }
32890fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor
32904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
32914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SpecificationRange.setEnd(T.getCloseLocation());
329279f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith  diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
329379f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith                                        Exceptions.empty());
329460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
32950fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor}
32966569d68745c8213709740337d2be52b031384f58Douglas Gregor
3297dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3298dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor/// function declaration.
3299ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorTypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
3300dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  assert(Tok.is(tok::arrow) && "expected arrow");
3301dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
3302dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  ConsumeToken();
3303dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
33047796eb5643244f3134834253ce5ea89107ac21c1Richard Smith  return ParseTypeName(&Range, Declarator::TrailingReturnContext);
3305dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor}
3306dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
33076569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \brief We have just started parsing the definition of a new class,
33086569d68745c8213709740337d2be52b031384f58Douglas Gregor/// so push that class onto our stack of classes that is currently
33096569d68745c8213709740337d2be52b031384f58Douglas Gregor/// being parsed.
3310eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCallSema::ParsingClassState
3311e402e72273cde2a64fa6097c1fe93f500038675dJohn McCallParser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3312e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall                         bool IsInterface) {
331326997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor  assert((NonNestedClass || !ClassStack.empty()) &&
33146569d68745c8213709740337d2be52b031384f58Douglas Gregor         "Nested class without outer class");
3315e402e72273cde2a64fa6097c1fe93f500038675dJohn McCall  ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
3316eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  return Actions.PushParsingClass();
33176569d68745c8213709740337d2be52b031384f58Douglas Gregor}
33186569d68745c8213709740337d2be52b031384f58Douglas Gregor
33196569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \brief Deallocate the given parsed class and all of its nested
33206569d68745c8213709740337d2be52b031384f58Douglas Gregor/// classes.
33216569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
3322d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3323d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    delete Class->LateParsedDeclarations[I];
33246569d68745c8213709740337d2be52b031384f58Douglas Gregor  delete Class;
33256569d68745c8213709740337d2be52b031384f58Douglas Gregor}
33266569d68745c8213709740337d2be52b031384f58Douglas Gregor
33276569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \brief Pop the top class of the stack of classes that are
33286569d68745c8213709740337d2be52b031384f58Douglas Gregor/// currently being parsed.
33296569d68745c8213709740337d2be52b031384f58Douglas Gregor///
33306569d68745c8213709740337d2be52b031384f58Douglas Gregor/// This routine should be called when we have finished parsing the
33316569d68745c8213709740337d2be52b031384f58Douglas Gregor/// definition of a class, but have not yet popped the Scope
33326569d68745c8213709740337d2be52b031384f58Douglas Gregor/// associated with the class's definition.
3333eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCallvoid Parser::PopParsingClass(Sema::ParsingClassState state) {
33346569d68745c8213709740337d2be52b031384f58Douglas Gregor  assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
33351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3336eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  Actions.PopParsingClass(state);
3337eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
33386569d68745c8213709740337d2be52b031384f58Douglas Gregor  ParsingClass *Victim = ClassStack.top();
33396569d68745c8213709740337d2be52b031384f58Douglas Gregor  ClassStack.pop();
33406569d68745c8213709740337d2be52b031384f58Douglas Gregor  if (Victim->TopLevelClass) {
33416569d68745c8213709740337d2be52b031384f58Douglas Gregor    // Deallocate all of the nested classes of this class,
33426569d68745c8213709740337d2be52b031384f58Douglas Gregor    // recursively: we don't need to keep any of this information.
33436569d68745c8213709740337d2be52b031384f58Douglas Gregor    DeallocateParsedClasses(Victim);
33446569d68745c8213709740337d2be52b031384f58Douglas Gregor    return;
33451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
33466569d68745c8213709740337d2be52b031384f58Douglas Gregor  assert(!ClassStack.empty() && "Missing top-level class?");
33476569d68745c8213709740337d2be52b031384f58Douglas Gregor
3348d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  if (Victim->LateParsedDeclarations.empty()) {
33496569d68745c8213709740337d2be52b031384f58Douglas Gregor    // The victim is a nested class, but we will not need to perform
33506569d68745c8213709740337d2be52b031384f58Douglas Gregor    // any processing after the definition of this class since it has
33516569d68745c8213709740337d2be52b031384f58Douglas Gregor    // no members whose handling was delayed. Therefore, we can just
33526569d68745c8213709740337d2be52b031384f58Douglas Gregor    // remove this nested class.
3353d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor    DeallocateParsedClasses(Victim);
33546569d68745c8213709740337d2be52b031384f58Douglas Gregor    return;
33556569d68745c8213709740337d2be52b031384f58Douglas Gregor  }
33566569d68745c8213709740337d2be52b031384f58Douglas Gregor
33576569d68745c8213709740337d2be52b031384f58Douglas Gregor  // This nested class has some members that will need to be processed
33586569d68745c8213709740337d2be52b031384f58Douglas Gregor  // after the top-level class is completely defined. Therefore, add
33596569d68745c8213709740337d2be52b031384f58Douglas Gregor  // it to the list of nested classes within its parent.
336023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
3361d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor  ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
336223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
33636569d68745c8213709740337d2be52b031384f58Douglas Gregor}
3364bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
3365c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3366c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///
3367c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith/// \return the parsed identifier on success, and 0 if the next token is not an
3368c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith/// attribute-token.
3369c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///
3370c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith/// C++11 [dcl.attr.grammar]p3:
3371c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///   If a keyword or an alternative token that satisfies the syntactic
3372c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///   requirements of an identifier is contained in an attribute-token,
3373c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///   it is considered an identifier.
3374c56298d87a9df507805a548d7d515e8b511df2c0Richard SmithIdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3375c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  switch (Tok.getKind()) {
3376c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  default:
3377c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    // Identifiers and keywords have identifier info attached.
33780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (!Tok.isAnnotation()) {
33790e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
33800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Loc = ConsumeToken();
33810e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        return II;
33820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
3383c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    }
33846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3385c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith
3386c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::ampamp:       // 'and'
3387c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::pipe:         // 'bitor'
3388c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::pipepipe:     // 'or'
3389c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::caret:        // 'xor'
3390c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::tilde:        // 'compl'
3391c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::amp:          // 'bitand'
3392c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::ampequal:     // 'and_eq'
3393c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::pipeequal:    // 'or_eq'
3394c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::caretequal:   // 'xor_eq'
3395c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::exclaim:      // 'not'
3396c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  case tok::exclaimequal: // 'not_eq'
3397c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    // Alternative tokens do not have identifier info, but their spelling
3398c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    // starts with an alphabetical character.
3399cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallString<8> SpellingBuf;
3400c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
34013f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose    if (isLetter(Spelling[0])) {
3402c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      Loc = ConsumeToken();
34030eb7526cd2524af78fb9a2a2522045fb25fc3d27Benjamin Kramer      return &PP.getIdentifierTable().get(Spelling);
3404c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    }
34056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3406c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  }
3407c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith}
3408c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith
34096880f492365cc4fa4c941aa83688635003ee7498Michael Hanstatic bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
34106880f492365cc4fa4c941aa83688635003ee7498Michael Han                                               IdentifierInfo *ScopeName) {
34116880f492365cc4fa4c941aa83688635003ee7498Michael Han  switch (AttributeList::getKind(AttrName, ScopeName,
34126880f492365cc4fa4c941aa83688635003ee7498Michael Han                                 AttributeList::AS_CXX11)) {
34136880f492365cc4fa4c941aa83688635003ee7498Michael Han  case AttributeList::AT_CarriesDependency:
34146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  case AttributeList::AT_Deprecated:
34156880f492365cc4fa4c941aa83688635003ee7498Michael Han  case AttributeList::AT_FallThrough:
3416cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  case AttributeList::AT_CXX11NoReturn: {
34176880f492365cc4fa4c941aa83688635003ee7498Michael Han    return true;
34186880f492365cc4fa4c941aa83688635003ee7498Michael Han  }
34196880f492365cc4fa4c941aa83688635003ee7498Michael Han
34206880f492365cc4fa4c941aa83688635003ee7498Michael Han  default:
34216880f492365cc4fa4c941aa83688635003ee7498Michael Han    return false;
34226880f492365cc4fa4c941aa83688635003ee7498Michael Han  }
34236880f492365cc4fa4c941aa83688635003ee7498Michael Han}
34246880f492365cc4fa4c941aa83688635003ee7498Michael Han
3425651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3426651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///
3427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// [C++11] attribute-argument-clause:
3428651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         '(' balanced-token-seq ')'
3429651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///
3430651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// [C++11] balanced-token-seq:
3431651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         balanced-token
3432651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         balanced-token-seq balanced-token
3433651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///
3434651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// [C++11] balanced-token:
3435651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         '(' balanced-token-seq ')'
3436651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         '[' balanced-token-seq ']'
3437651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         '{' balanced-token-seq '}'
3438651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///         any token but '(', ')', '[', ']', '{', or '}'
3439651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesbool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3440651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     SourceLocation AttrNameLoc,
3441651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     ParsedAttributes &Attrs,
3442651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     SourceLocation *EndLoc,
3443651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     IdentifierInfo *ScopeName,
3444651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     SourceLocation ScopeLoc) {
3445651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
34466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  SourceLocation LParenLoc = Tok.getLocation();
3447651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3448651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // If the attribute isn't known, we will not attempt to parse any
3449651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // arguments.
3450651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3451651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                    getTargetInfo().getTriple(), getLangOpts())) {
3452651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Eat the left paren, then skip to the ending right paren.
3453651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ConsumeParen();
3454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SkipUntil(tok::r_paren);
3455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return false;
3456651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
3457651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3458651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ScopeName && ScopeName->getName() == "gnu")
3459651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // GNU-scoped attributes have some special cases to handle GNU-specific
3460651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // behaviors.
3461651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
34626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                          ScopeLoc, AttributeList::AS_CXX11, nullptr);
34636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  else {
34646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    unsigned NumArgs =
34656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
34666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
34676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
34686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const AttributeList *Attr = Attrs.getList();
34696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
34706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // If the attribute is a standard or built-in attribute and we are
34716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // parsing an argument list, we need to determine whether this attribute
34726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // was allowed to have an argument list (such as [[deprecated]]), and how
34736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // many arguments were parsed (so we can diagnose on [[deprecated()]]).
3474c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      if (Attr->getMaxArgs() && !NumArgs) {
3475c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        // The attribute was allowed to have arguments, but none were provided
3476c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        // even though the attribute parsed successfully. This is an error.
3477c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
3478c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      } else if (!Attr->getMaxArgs()) {
3479c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        // The attribute parsed successfully, but was not allowed to have any
3480c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        // arguments. It doesn't matter whether any were provided -- the
34816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        // presence of the argument list (even if empty) is diagnosed.
34826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
34830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            << AttrName
34840e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
34856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
34866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
34876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
3488651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return true;
3489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
3490651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3491651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
3492bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///
34936ee326af4e77e6f05973486097884d7431f2108dRichard Smith/// [C++11] attribute-specifier:
3494bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         '[' '[' attribute-list ']' ']'
349582d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne///         alignment-specifier
3496bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///
34976ee326af4e77e6f05973486097884d7431f2108dRichard Smith/// [C++11] attribute-list:
3498bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         attribute[opt]
3499bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         attribute-list ',' attribute[opt]
3500c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///         attribute '...'
3501c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith///         attribute-list ',' attribute '...'
3502bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///
35036ee326af4e77e6f05973486097884d7431f2108dRichard Smith/// [C++11] attribute:
3504bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         attribute-token attribute-argument-clause[opt]
3505bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///
35066ee326af4e77e6f05973486097884d7431f2108dRichard Smith/// [C++11] attribute-token:
3507bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         identifier
3508bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         attribute-scoped-token
3509bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///
35106ee326af4e77e6f05973486097884d7431f2108dRichard Smith/// [C++11] attribute-scoped-token:
3511bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         attribute-namespace '::' identifier
3512bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///
35136ee326af4e77e6f05973486097884d7431f2108dRichard Smith/// [C++11] attribute-namespace:
3514bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt///         identifier
3515c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithvoid Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
35163497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne                                          SourceLocation *endLoc) {
351782d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne  if (Tok.is(tok::kw_alignas)) {
351841be673e93ed225b45479557b20ff19b3082bae8Richard Smith    Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
351982d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    ParseAlignmentSpecifier(attrs, endLoc);
352082d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    return;
352182d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne  }
352282d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne
3523bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
35246ee326af4e77e6f05973486097884d7431f2108dRichard Smith      && "Not a C++11 attribute list");
3525bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
352641be673e93ed225b45479557b20ff19b3082bae8Richard Smith  Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
352741be673e93ed225b45479557b20ff19b3082bae8Richard Smith
3528bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  ConsumeBracket();
3529bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  ConsumeBracket();
3530193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
3531cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3532cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
3533c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  while (Tok.isNot(tok::r_square)) {
3534bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    // attribute not present
3535651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TryConsumeToken(tok::comma))
3536bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      continue;
3537bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
3538c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    SourceLocation ScopeLoc, AttrLoc;
35396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
3540c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith
3541c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3542c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    if (!AttrName)
3543c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      // Break out to the "expected ']'" diagnostic.
3544c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      break;
3545193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam
3546bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    // scoped attribute
3547651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TryConsumeToken(tok::coloncolon)) {
3548c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      ScopeName = AttrName;
3549c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      ScopeLoc = AttrLoc;
3550c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith
3551c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3552c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith      if (!AttrName) {
3553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
35548fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
3555bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt        continue;
3556bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      }
3557bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    }
3558bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
3559651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
3560bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    bool AttrParsed = false;
35616880f492365cc4fa4c941aa83688635003ee7498Michael Han
3562cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (StandardAttr &&
3563cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3564cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3565651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << AttrName << SourceRange(SeenAttrs[AttrName]);
3566cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
35676880f492365cc4fa4c941aa83688635003ee7498Michael Han    // Parse attribute arguments
35686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (Tok.is(tok::l_paren))
3569651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3570651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                           ScopeName, ScopeLoc);
3571bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
35726880f492365cc4fa4c941aa83688635003ee7498Michael Han    if (!AttrParsed)
3573e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith      attrs.addNew(AttrName,
3574e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith                   SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3575e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith                               AttrLoc),
35766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                   ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
35776ee326af4e77e6f05973486097884d7431f2108dRichard Smith
3578651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TryConsumeToken(tok::ellipsis))
35796880f492365cc4fa4c941aa83688635003ee7498Michael Han      Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
35806880f492365cc4fa4c941aa83688635003ee7498Michael Han        << AttrName->getName();
3581bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  }
3582bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
3583651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ExpectAndConsume(tok::r_square))
35848fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_square);
35853497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne  if (endLoc)
35863497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne    *endLoc = Tok.getLocation();
3587651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ExpectAndConsume(tok::r_square))
35888fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_square);
35893497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne}
35903497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne
35912edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
35923497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne///
35933497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne/// attribute-specifier-seq:
35943497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne///       attribute-specifier-seq[opt] attribute-specifier
3595c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithvoid Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
35963497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne                                  SourceLocation *endLoc) {
3597672edb0a04a5273e3a501f3b196844c125290780Richard Smith  assert(getLangOpts().CPlusPlus11);
3598672edb0a04a5273e3a501f3b196844c125290780Richard Smith
35993497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne  SourceLocation StartLoc = Tok.getLocation(), Loc;
36003497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne  if (!endLoc)
36013497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne    endLoc = &Loc;
36023497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne
36038828ee7faa42f889ade3bb635dc5f1338be671b1Douglas Gregor  do {
3604c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith    ParseCXX11AttributeSpecifier(attrs, endLoc);
36056ee326af4e77e6f05973486097884d7431f2108dRichard Smith  } while (isCXX11AttributeSpecifier());
3606bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
36073497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne  attrs.Range = SourceRange(StartLoc, *endLoc);
3608bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt}
3609bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
36105eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithvoid Parser::DiagnoseAndSkipCXX11Attributes() {
36115eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  // Start and end location of an attribute or an attribute list.
36125eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  SourceLocation StartLoc = Tok.getLocation();
3613c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  SourceLocation EndLoc = SkipCXX11Attributes();
3614c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
3615c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  if (EndLoc.isValid()) {
3616c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    SourceRange Range(StartLoc, EndLoc);
3617c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    Diag(StartLoc, diag::err_attributes_not_allowed)
3618c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      << Range;
3619c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  }
3620c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines}
3621c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
3622c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen HinesSourceLocation Parser::SkipCXX11Attributes() {
36235eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  SourceLocation EndLoc;
36245eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith
3625c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  if (!isCXX11AttributeSpecifier())
3626c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    return EndLoc;
3627c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
36285eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  do {
36295eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    if (Tok.is(tok::l_square)) {
36305eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      BalancedDelimiterTracker T(*this, tok::l_square);
36315eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      T.consumeOpen();
36325eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      T.skipToEnd();
36335eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      EndLoc = T.getCloseLocation();
36345eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    } else {
36355eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
36365eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      ConsumeToken();
36375eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      BalancedDelimiterTracker T(*this, tok::l_paren);
36385eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      if (!T.consumeOpen())
36395eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith        T.skipToEnd();
36405eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      EndLoc = T.getCloseLocation();
36415eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith    }
36425eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  } while (isCXX11AttributeSpecifier());
36435eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith
3644c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  return EndLoc;
36455eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith}
36465eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith
3647334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3648334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet///
3649334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// [MS] ms-attribute:
3650334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet///             '[' token-seq ']'
3651334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet///
3652334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// [MS] ms-attribute-seq:
3653334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet///             ms-attribute[opt]
3654334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet///             ms-attribute ms-attribute-seq
36557f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallvoid Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
36567f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                      SourceLocation *endLoc) {
3657334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet  assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3658334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet
3659334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet  while (Tok.is(tok::l_square)) {
36606ee326af4e77e6f05973486097884d7431f2108dRichard Smith    // FIXME: If this is actually a C++11 attribute, parse it as one.
3661334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet    ConsumeBracket();
36628fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
36637f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (endLoc) *endLoc = Tok.getLocation();
3664651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ExpectAndConsume(tok::r_square);
3665334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet  }
3666334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet}
3667563a645de82231a55e221fe655b7188bf8369662Francois Pichet
3668563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3669563a645de82231a55e221fe655b7188bf8369662Francois Pichet                                                    AccessSpecifier& CurAS) {
36703896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  IfExistsCondition Result;
3671563a645de82231a55e221fe655b7188bf8369662Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
3672563a645de82231a55e221fe655b7188bf8369662Francois Pichet    return;
3673563a645de82231a55e221fe655b7188bf8369662Francois Pichet
36743896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  BalancedDelimiterTracker Braces(*this, tok::l_brace);
36753896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Braces.consumeOpen()) {
3676651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(Tok, diag::err_expected) << tok::l_brace;
3677563a645de82231a55e221fe655b7188bf8369662Francois Pichet    return;
3678563a645de82231a55e221fe655b7188bf8369662Francois Pichet  }
3679563a645de82231a55e221fe655b7188bf8369662Francois Pichet
36803896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  switch (Result.Behavior) {
36813896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Parse:
36823896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    // Parse the declarations below.
36833896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
36843896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
36853896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Dependent:
36863896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
36873896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor      << Result.IsIfExists;
36883896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    // Fall through to skip.
36893896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
36903896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Skip:
36913896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Braces.skipToEnd();
3692563a645de82231a55e221fe655b7188bf8369662Francois Pichet    return;
3693563a645de82231a55e221fe655b7188bf8369662Francois Pichet  }
3694563a645de82231a55e221fe655b7188bf8369662Francois Pichet
3695651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
3696563a645de82231a55e221fe655b7188bf8369662Francois Pichet    // __if_exists, __if_not_exists can nest.
3697563a645de82231a55e221fe655b7188bf8369662Francois Pichet    if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3698563a645de82231a55e221fe655b7188bf8369662Francois Pichet      ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3699563a645de82231a55e221fe655b7188bf8369662Francois Pichet      continue;
3700563a645de82231a55e221fe655b7188bf8369662Francois Pichet    }
3701563a645de82231a55e221fe655b7188bf8369662Francois Pichet
3702563a645de82231a55e221fe655b7188bf8369662Francois Pichet    // Check for extraneous top-level semicolon.
3703563a645de82231a55e221fe655b7188bf8369662Francois Pichet    if (Tok.is(tok::semi)) {
3704eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      ConsumeExtraSemi(InsideStruct, TagType);
3705563a645de82231a55e221fe655b7188bf8369662Francois Pichet      continue;
3706563a645de82231a55e221fe655b7188bf8369662Francois Pichet    }
3707563a645de82231a55e221fe655b7188bf8369662Francois Pichet
3708563a645de82231a55e221fe655b7188bf8369662Francois Pichet    AccessSpecifier AS = getAccessSpecifierIfPresent();
3709563a645de82231a55e221fe655b7188bf8369662Francois Pichet    if (AS != AS_none) {
3710563a645de82231a55e221fe655b7188bf8369662Francois Pichet      // Current token is a C++ access specifier.
3711563a645de82231a55e221fe655b7188bf8369662Francois Pichet      CurAS = AS;
3712563a645de82231a55e221fe655b7188bf8369662Francois Pichet      SourceLocation ASLoc = Tok.getLocation();
3713563a645de82231a55e221fe655b7188bf8369662Francois Pichet      ConsumeToken();
3714563a645de82231a55e221fe655b7188bf8369662Francois Pichet      if (Tok.is(tok::colon))
3715563a645de82231a55e221fe655b7188bf8369662Francois Pichet        Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3716563a645de82231a55e221fe655b7188bf8369662Francois Pichet      else
3717651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag(Tok, diag::err_expected) << tok::colon;
3718563a645de82231a55e221fe655b7188bf8369662Francois Pichet      ConsumeToken();
3719563a645de82231a55e221fe655b7188bf8369662Francois Pichet      continue;
3720563a645de82231a55e221fe655b7188bf8369662Francois Pichet    }
3721563a645de82231a55e221fe655b7188bf8369662Francois Pichet
3722563a645de82231a55e221fe655b7188bf8369662Francois Pichet    // Parse all the comma separated declarators.
37236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ParseCXXClassMemberDeclaration(CurAS, nullptr);
3724563a645de82231a55e221fe655b7188bf8369662Francois Pichet  }
37253896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
37263896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Braces.consumeClose();
3727563a645de82231a55e221fe655b7188bf8369662Francois Pichet}
3728