13d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
23d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//
33d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//                     The LLVM Compiler Infrastructure
43d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//
53d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet// This file is distributed under the University of Illinois Open Source
63d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet// License. See LICENSE.TXT for details.
73d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//
83d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//===----------------------------------------------------------------------===//
93d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//
103d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet// This file implements C++ semantic analysis for scope specifiers.
113d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//
123d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet//===----------------------------------------------------------------------===//
133d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "TypeLocBuilder.h"
163d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet#include "clang/AST/ASTContext.h"
1742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor#include "clang/AST/DeclTemplate.h"
18fe85cedd58df7daed29201703cfb8806e12876d0Douglas Gregor#include "clang/AST/ExprCXX.h"
19e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor#include "clang/AST/NestedNameSpecifier.h"
20b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
2119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Lookup.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.h"
243d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet#include "llvm/ADT/STLExtras.h"
257551c183e8d788b5254e9c6f8bd8d719cea47da8Douglas Gregor#include "llvm/Support/raw_ostream.h"
263d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetusing namespace clang;
273d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
2843d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor/// \brief Find the current instantiation that associated with the given type.
29f62c690c55fb920f3ba0a4f6e6e259e2ae7ce297Richard Smithstatic CXXRecordDecl *getCurrentInstantiationOf(QualType T,
30d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor                                                DeclContext *CurContext) {
3143d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor  if (T.isNull())
326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3331f17ecbef57b5679c017c375db330546b7b5145John McCall
3431f17ecbef57b5679c017c375db330546b7b5145John McCall  const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
35d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
36d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
37f62c690c55fb920f3ba0a4f6e6e259e2ae7ce297Richard Smith    if (!Record->isDependentContext() ||
38f62c690c55fb920f3ba0a4f6e6e259e2ae7ce297Richard Smith        Record->isCurrentInstantiation(CurContext))
39d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor      return Record;
40d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor
416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
42d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  } else if (isa<InjectedClassNameType>(Ty))
4331f17ecbef57b5679c017c375db330546b7b5145John McCall    return cast<InjectedClassNameType>(Ty)->getDecl();
4431f17ecbef57b5679c017c375db330546b7b5145John McCall  else
456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
4643d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor}
4743d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor
482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \brief Compute the DeclContext that is associated with the given type.
492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
502dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param T the type for which we are attempting to find a DeclContext.
512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \returns the declaration context represented by the type T,
532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// or NULL if the declaration context cannot be computed (e.g., because it is
542dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// dependent and not the current instantiation).
552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas GregorDeclContext *Sema::computeDeclContext(QualType T) {
56d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  if (!T->isDependentType())
57d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    if (const TagType *Tag = T->getAs<TagType>())
58d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor      return Tag->getDecl();
591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  return ::getCurrentInstantiationOf(T, CurContext);
612dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor}
622dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
63e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor/// \brief Compute the DeclContext that is associated with the given
64e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor/// scope specifier.
65f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor///
66f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// \param SS the C++ scope specifier as it appears in the source
67f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor///
68f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// \param EnteringContext when true, we will be entering the context of
69f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// this scope specifier, so we can retrieve the declaration context of a
70f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// class template or class template partial specialization even if it is
71f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// not the current instantiation.
72f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor///
73f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// \returns the declaration context represented by the scope specifier @p SS,
74f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// or NULL if the declaration context cannot be computed (e.g., because it is
75f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// dependent and not the current instantiation).
76f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas GregorDeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
77f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor                                      bool EnteringContext) {
78e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (!SS.isSet() || SS.isInvalid())
796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
80ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor
81c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith  NestedNameSpecifier *NNS = SS.getScopeRep();
8242af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  if (NNS->isDependent()) {
8342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    // If this nested-name-specifier refers to the current
8442af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    // instantiation, return its DeclContext.
8542af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
8642af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor      return Record;
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor    if (EnteringContext) {
893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      const Type *NNSType = NNS->getAsType();
903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (!NNSType) {
916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      }
933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      // Look through type alias templates, per C++0x [temp.dep.type]p1.
953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      NNSType = Context.getCanonicalType(NNSType);
963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      if (const TemplateSpecializationType *SpecType
973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith            = NNSType->getAs<TemplateSpecializationType>()) {
98495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // We are entering the context of the nested name specifier, so try to
99495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // match the nested name specifier to either a primary class template
100495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // or a class template partial specialization.
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateDecl *ClassTemplate
102f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor              = dyn_cast_or_null<ClassTemplateDecl>(
103f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor                            SpecType->getTemplateName().getAsTemplateDecl())) {
104b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          QualType ContextType
105b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor            = Context.getCanonicalType(QualType(SpecType, 0));
106b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor
107f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor          // If the type of the nested name specifier is the same as the
108f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor          // injected class name of the named class template, we're entering
109f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor          // into that class template definition.
1103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          QualType Injected
11124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor            = ClassTemplate->getInjectedClassNameSpecialization();
112b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          if (Context.hasSameType(Injected, ContextType))
113f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor            return ClassTemplate->getTemplatedDecl();
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // If the type of the nested name specifier is the same as the
116b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // type of one of the class template's class template partial
117b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // specializations, we're entering into the definition of that
118b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // class template partial specialization.
119b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          if (ClassTemplatePartialSpecializationDecl *PartialSpec
120b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                = ClassTemplate->findPartialSpecialization(ContextType))
121b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor            return PartialSpec;
122f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor        }
1233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
124495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // The nested name specifier refers to a member of a class template.
125495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        return RecordT->getDecl();
126f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor      }
127f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor    }
1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
13042af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  }
131ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
132ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  switch (NNS->getKind()) {
133ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::Identifier:
134b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
135ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
136ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::Namespace:
137ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    return NNS->getAsNamespace();
138ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
13914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
14014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NNS->getAsNamespaceAlias()->getNamespace();
14114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
142ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::TypeSpec:
143ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
144edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    const TagType *Tag = NNS->getAsType()->getAs<TagType>();
145edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    assert(Tag && "Non-tag type in nested-name-specifier");
146edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    return Tag->getDecl();
1477530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
148ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
149ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::Global:
150ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    return Context.getTranslationUnitDecl();
151ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  }
152ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
1537530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
154ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor}
155ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor
1565953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregorbool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
1575953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  if (!SS.isSet() || SS.isInvalid())
1585953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    return false;
1595953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
160c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith  return SS.getScopeRep()->isDependent();
1615953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor}
1625953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
16342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// \brief If the given nested name specifier refers to the current
16442af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// instantiation, return the declaration that corresponds to that
16542af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// current instantiation (C++0x [temp.dep.type]p1).
16642af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor///
16742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// \param NNS a dependent nested name specifier.
16842af25f865a82022a04bedeb483ac251c4412e29Douglas GregorCXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
1694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "Only callable in C++");
17042af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
17142af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
172f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor  if (!NNS->getAsType())
1736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1751560def1f796c0e5db6026fe366759623c9f13c2Douglas Gregor  QualType T = QualType(NNS->getAsType(), 0);
176d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  return ::getCurrentInstantiationOf(T, CurContext);
17742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor}
17842af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
1794fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// \brief Require that the context specified by SS be complete.
1804fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor///
1814fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// If SS refers to a type, this routine checks whether the type is
1824fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// complete enough (or can be made complete enough) for name lookup
1834fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// into the DeclContext. A type that is not yet completed can be
1844fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// considered "complete enough" if it is a class/struct/union/enum
1854fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// that is currently being defined. Or, if we have a type that names
1864fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// a class template specialization that is not a complete type, we
1874fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// will attempt to instantiate that class template.
18877bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCallbool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
18977bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall                                      DeclContext *DC) {
1906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(DC && "given null context");
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
192f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  TagDecl *tag = dyn_cast<TagDecl>(DC);
1939dc71d2fddcd283e07d45f3894c8559e2f7dd9a7John McCall
194f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // If this is a dependent type, then we consider it complete.
195f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (!tag || tag->isDependentContext())
196f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
197f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
198f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // If we're currently defining this type, then lookup into the
199f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // type is okay: don't complain that it isn't complete yet.
200f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  QualType type = Context.getTypeDeclType(tag);
201f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  const TagType *tagType = type->getAs<TagType>();
202f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (tagType && tagType->isBeingDefined())
203f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
204f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
205f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SourceLocation loc = SS.getLastQualifierNameLoc();
206f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (loc.isInvalid()) loc = SS.getRange().getBegin();
207f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
208f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // The type must be complete.
209d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
210d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                          SS.getRange())) {
211f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    SS.SetInvalid(SS.getRange());
212f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return true;
2134fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor  }
2144fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor
215f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // Fixed enum types are complete, but they aren't valid as scopes
216f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // until we see a definition, so awkwardly pull out this special
217f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // case.
218f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
219f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (!enumType || enumType->getDecl()->isCompleteDefinition())
220f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
221f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
222f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // Try to instantiate the definition, if this is a specialization of an
223f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // enumeration temploid.
224f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  EnumDecl *ED = enumType->getDecl();
225f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
226f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
2271af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
2281af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
2291af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith                          TSK_ImplicitInstantiation)) {
2301af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith        SS.SetInvalid(SS.getRange());
2311af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith        return true;
2321af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      }
2331af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      return false;
2341af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    }
235f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  }
236f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
237f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Diag(loc, diag::err_incomplete_nested_name_spec)
238f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    << type << SS.getRange();
239f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SS.SetInvalid(SS.getRange());
240f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return true;
2414fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor}
2423d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
2432e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
2442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                        CXXScopeSpec &SS) {
2452e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  SS.MakeGlobal(Context, CCLoc);
2462e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return false;
2473d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
2483d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
2492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \brief Determines whether the given declaration is an valid acceptable
2502dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// result for name lookup of a nested-name-specifier.
25189cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenkobool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD) {
2522dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (!SD)
2532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return false;
2541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Namespace and namespace aliases are fine.
2562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
2572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (!isa<TypeDecl>(SD))
2602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return false;
2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2626b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith  // Determine whether we have a class (or, in C++11, an enum) or
2632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // a typedef thereof. If so, build the nested-name-specifier.
2642dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
2652dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (T->isDependentType())
2662dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
26789cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
2682dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (TD->getUnderlyingType()->isRecordType() ||
26980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        (Context.getLangOpts().CPlusPlus11 &&
2702dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor         TD->getUnderlyingType()->isEnumeralType()))
2712dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      return true;
2721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (isa<RecordDecl>(SD) ||
27380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith             (Context.getLangOpts().CPlusPlus11 && isa<EnumDecl>(SD)))
2742dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
2752dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
2762dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  return false;
2772dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor}
2782dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
279c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// \brief If the given nested-name-specifier begins with a bare identifier
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// (e.g., Base::), perform name lookup for that identifier as a
281c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// nested-name-specifier within the given scope, and return the result of that
282c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// name lookup.
283c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas GregorNamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
284c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (!S || !NNS)
2856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
287c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  while (NNS->getPrefix())
288c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    NNS = NNS->getPrefix();
2891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
290c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (NNS->getKind() != NestedNameSpecifier::Identifier)
2916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
293a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
294a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall                     LookupNestedNameSpecifierName);
295a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupName(Found, S);
296c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
297c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
2981bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (!Found.isSingleResult())
2996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3001bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall
3011bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  NamedDecl *Result = Found.getFoundDecl();
302edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor  if (isAcceptableNestedNameSpecifier(Result))
303c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    return Result;
3041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
306c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor}
307c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
3089ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
30977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                        SourceLocation IdLoc,
31077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                        IdentifierInfo &II,
311b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectTypePtr) {
31277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
31377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
31477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
31577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  // Determine where to perform name lookup
3166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DeclContext *LookupCtx = nullptr;
31777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  bool isDependent = false;
31877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (!ObjectType.isNull()) {
31977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // This nested-name-specifier occurs in a member access expression, e.g.,
32077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // x->B::f, and we are looking into the type of the object.
32177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
32277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupCtx = computeDeclContext(ObjectType);
32377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    isDependent = ObjectType->isDependentType();
32477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else if (SS.isSet()) {
32577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // This nested-name-specifier occurs after another nested-name-specifier,
32677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // so long into the context associated with the prior nested-name-specifier.
32777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupCtx = computeDeclContext(SS, false);
32877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    isDependent = isDependentScopeSpecifier(SS);
32977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    Found.setContextRange(SS.getRange());
33077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
33177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
33277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (LookupCtx) {
33377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // Perform "qualified" name lookup into the declaration context we
33477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // computed, which is either the type of the base of a member access
33577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // expression or the declaration context associated with a prior
33677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // nested-name-specifier.
33777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
33877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // The declaration context must be complete.
33977bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall    if (!LookupCtx->isDependentContext() &&
34077bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall        RequireCompleteDeclContext(SS, LookupCtx))
34177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      return false;
34277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
34377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupQualifiedName(Found, LookupCtx);
34477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else if (isDependent) {
34577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    return false;
34677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else {
34777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupName(Found, S);
34877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
34977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  Found.suppressDiagnostics();
35077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
35177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
35277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
35377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
35477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  return false;
35577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor}
35677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
3573b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrainnamespace {
3583b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3593b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain// Callback to only accept typo corrections that can be a valid C++ member
3603b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain// intializer: either a non-static field member or a base class.
3613b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrainclass NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
3623b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain public:
3633b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
3643b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain      : SRef(SRef) {}
3653b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
366651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ValidateCandidate(const TypoCorrection &candidate) override {
3673b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain    return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
3683b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  }
3693b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3703b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain private:
3713b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  Sema &SRef;
3723b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain};
3733b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3743b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain}
3753b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
376c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// \brief Build a new nested-name-specifier for "identifier::", as described
377c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// by ActOnCXXNestedNameSpecifier.
378c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor///
379c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
3806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// that it contains an extra parameter \p ScopeLookupResult.
3816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///
3826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param S Scope in which the nested-name-specifier occurs.
3836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param Identifier Identifier in the sequence "identifier" "::".
3846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param IdentifierLoc Location of the \p Identifier.
3856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param CCLoc Location of "::" following Identifier.
3866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param ObjectType Type of postfix expression if the nested-name-specifier
3876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        occurs in construct like: <tt>ptr->nns::f</tt>.
3886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param EnteringContext If true, enter the context specified by the
3896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        nested-name-specifier.
3906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param SS Optional nested name specifier preceding the identifier.
3916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param ScopeLookupResult Provides the result of name lookup within the
3926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        scope of the nested-name-specifier that was computed at template
3936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        definition time.
3946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param ErrorRecoveryLookup Specifies if the method is called to improve
3956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        error recovery and what kind of recovery is performed.
3966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param IsCorrectedToColon If not null, suggestion of replace '::' -> ':'
3976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        are allowed.  The bool value pointed by this parameter is set to
3986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///       'true' if the identifier is treated as if it was followed by ':',
3996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        not '::'.
4006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///
4016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
402c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// that it contains an extra parameter \p ScopeLookupResult, which provides
403c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// the result of name lookup within the scope of the nested-name-specifier
404a6e51993362fcd53c13c2fa30a288d6fcbce4de6Douglas Gregor/// that was computed at template definition time.
40546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner///
40646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// If ErrorRecoveryLookup is true, then this call is used to improve error
40746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// recovery.  This means that it should not emit diagnostics, it should
4082e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// just return true on failure.  It also means it should only return a valid
40946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// scope if it *knows* that the result is correct.  It should not return in a
4102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// dependent context, for example. Nor will it extend \p SS with the scope
4112e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// specifier.
4122e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::BuildCXXNestedNameSpecifier(Scope *S,
4132e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       IdentifierInfo &Identifier,
4142e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation IdentifierLoc,
4152e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
4162e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       QualType ObjectType,
4172e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool EnteringContext,
4182e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       CXXScopeSpec &SS,
4192e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       NamedDecl *ScopeLookupResult,
4206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool ErrorRecoveryLookup,
4216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool *IsCorrectedToColon) {
4222e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  LookupResult Found(*this, &Identifier, IdentifierLoc,
4232e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                     LookupNestedNameSpecifierName);
424a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall
4252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Determine where to perform name lookup
4266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DeclContext *LookupCtx = nullptr;
4272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  bool isDependent = false;
4286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (IsCorrectedToColon)
4296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    *IsCorrectedToColon = false;
430c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (!ObjectType.isNull()) {
4312dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // This nested-name-specifier occurs in a member access expression, e.g.,
4322dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // x->B::f, and we are looking into the type of the object.
4332dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
4342dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    LookupCtx = computeDeclContext(ObjectType);
4352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    isDependent = ObjectType->isDependentType();
4362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  } else if (SS.isSet()) {
4372dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // This nested-name-specifier occurs after another nested-name-specifier,
4383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // so look into the context associated with the prior nested-name-specifier.
4392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    LookupCtx = computeDeclContext(SS, EnteringContext);
4402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    isDependent = isDependentScopeSpecifier(SS);
441a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    Found.setContextRange(SS.getRange());
4422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4442dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  bool ObjectTypeSearchedInScope = false;
4452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (LookupCtx) {
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Perform "qualified" name lookup into the declaration context we
4472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // computed, which is either the type of the base of a member access
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // expression or the declaration context associated with a prior
4492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // nested-name-specifier.
4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // The declaration context must be complete.
45277bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall    if (!LookupCtx->isDependentContext() &&
45377bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall        RequireCompleteDeclContext(SS, LookupCtx))
4542e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
456a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupQualifiedName(Found, LookupCtx);
4571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
458a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    if (!ObjectType.isNull() && Found.empty()) {
4592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p4:
4602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the id-expression in a class member access is a qualified-id of
4611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the form
4622dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
4632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //        class-name-or-namespace-name::...
4642dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the class-name-or-namespace-name following the . or -> operator is
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   looked up both in the context of the entire postfix-expression and in
4672dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the scope of the class of the object expression. If the name is found
4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   only in the scope of the class of the object expression, the name
4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   shall refer to a class-name. If the name is found only in the
4702dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   context of the entire postfix-expression, the name shall refer to a
4712dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name. [...]
4722dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
4732dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // Qualified name lookup into a class will not find a namespace-name,
474714c992099b3b9442759f29038bb3f2c5a59a23dDouglas Gregor      // so we do not need to diagnose that case specifically. However,
4752dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // this qualified name lookup may find nothing. In that case, perform
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // unqualified name lookup in the given scope (if available) or
477c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // reconstruct the result from when name lookup was performed at template
478c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // definition time.
479c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      if (S)
480a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall        LookupName(Found, S);
481f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      else if (ScopeLookupResult)
482f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        Found.addDecl(ScopeLookupResult);
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4842dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      ObjectTypeSearchedInScope = true;
4852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
486ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  } else if (!isDependent) {
487ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor    // Perform unqualified name lookup in the current scope.
488ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor    LookupName(Found, S);
489ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  }
490ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor
491ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  // If we performed lookup into a dependent context and did not find anything,
492ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  // that's fine: just build a dependent nested-name-specifier.
493ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  if (Found.empty() && isDependent &&
494ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor      !(LookupCtx && LookupCtx->isRecord() &&
495ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor        (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
496ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor         !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
49746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // Don't speculate if we're just trying to improve error recovery.
49846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    if (ErrorRecoveryLookup)
4992e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
5006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5012dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // We were not able to compute the declaration context for a dependent
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // base object type or prior nested-name-specifier, so this
5032dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // nested-name-specifier refers to an unknown specialization. Just build
5042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // a dependent nested-name-specifier.
5052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
5062e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
5076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5092dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // FIXME: Deal with ambiguities cleanly.
510175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor
5116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Found.empty() && !ErrorRecoveryLookup) {
5126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // If identifier is not found as class-name-or-namespace-name, but is found
5136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // as other entity, don't look for typos.
5146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
5156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (LookupCtx)
5166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      LookupQualifiedName(R, LookupCtx);
5176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    else if (S && !isDependent)
5186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      LookupName(R, S);
5196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!R.empty()) {
5206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // The identifier is found in ordinary lookup. If correction to colon is
5216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // allowed, suggest replacement to ':'.
5226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (IsCorrectedToColon) {
5236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        *IsCorrectedToColon = true;
5246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Diag(CCLoc, diag::err_nested_name_spec_is_not_class)
5256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            << &Identifier << getLangOpts().CPlusPlus
5266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            << FixItHint::CreateReplacement(CCLoc, ":");
5276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
5286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Diag(ND->getLocation(), diag::note_declared_at);
5296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return true;
5306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
5316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // Replacement '::' -> ':' is not allowed, just issue respective error.
5326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Diag(R.getNameLoc(), diag::err_expected_class_or_namespace)
5336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          << &Identifier << getLangOpts().CPlusPlus;
5346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
535ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        Diag(ND->getLocation(), diag::note_entity_declared_at) << &Identifier;
5366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return true;
5376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
5386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
540651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
541175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    // We haven't found anything, and we're not recovering from a
542175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    // different kind of error, so look for typos.
543175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    DeclarationName Name = Found.getLookupName();
5443b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain    NestedNameSpecifierValidatorCCC Validator(*this);
545d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    Found.clear();
5462d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith    if (TypoCorrection Corrected =
5472d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith            CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S,
5486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                        &SS, Validator, CTK_ErrorRecovery, LookupCtx,
5496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                        EnteringContext)) {
5502d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith      if (LookupCtx) {
5512d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith        bool DroppedSpecifier =
5522d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith            Corrected.WillReplaceSpecifier() &&
5532d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith            Name.getAsString() == Corrected.getAsString(getLangOpts());
5549a5d6bb0c26215adddc5ce00f21d863160cbd0b7Bill Wendling        if (DroppedSpecifier)
5559a5d6bb0c26215adddc5ce00f21d863160cbd0b7Bill Wendling          SS.clear();
5562d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith        diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
5572d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith                                  << Name << LookupCtx << DroppedSpecifier
5582d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith                                  << SS.getRange());
5592d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith      } else
5602d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith        diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
5612d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith                                  << Name);
5622d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith
5632d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith      if (NamedDecl *ND = Corrected.getCorrectionDecl())
564d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor        Found.addDecl(ND);
565d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      Found.setLookupName(Corrected.getCorrection());
56612eb5d6aa882eb247a6c22225b625eee04217105Douglas Gregor    } else {
5672e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      Found.setLookupName(&Identifier);
56812eb5d6aa882eb247a6c22225b625eee04217105Douglas Gregor    }
569175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor  }
570175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor
5711bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  NamedDecl *SD = Found.getAsSingle<NamedDecl>();
572edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor  if (isAcceptableNestedNameSpecifier(SD)) {
57305e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor    if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
57480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        !getLangOpts().CPlusPlus11) {
57505e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      // C++03 [basic.lookup.classref]p4:
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If the name is found in both contexts, the
5772dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name-or-namespace-name shall refer to the same entity.
5782dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
5792dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // We already found the name in the scope of the object. Now, look
5802dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // into the current scope (the scope of the postfix-expression) to
581c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // see if we can find the same name there. As above, if there is no
582c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // scope, reconstruct the result from the template instantiation itself.
58305e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      //
58405e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      // Note that C++11 does *not* perform this redundant lookup.
585f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      NamedDecl *OuterDecl;
586f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      if (S) {
5872e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
5882e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                LookupNestedNameSpecifierName);
589a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall        LookupName(FoundOuter, S);
5901bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall        OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
591f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      } else
592f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        OuterDecl = ScopeLookupResult;
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
594edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor      if (isAcceptableNestedNameSpecifier(OuterDecl) &&
5952dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor          OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
5962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor          (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
5972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor           !Context.hasSameType(
598c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor                            Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
5992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                               Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
6006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (ErrorRecoveryLookup)
6016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return true;
6022e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
6032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(IdentifierLoc,
6042e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor              diag::err_nested_name_member_ref_lookup_ambiguous)
6052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           << &Identifier;
6062e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
6072e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           << ObjectType;
6082e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
6092e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
6102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         // Fall through so that we'll pick the name we found in the object
6112e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         // type, since that's probably what the user wanted anyway.
6122e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor       }
6132dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // If we're just performing this lookup for error-recovery purposes,
6162e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    // don't extend the nested-name-specifier. Just return now.
6172e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (ErrorRecoveryLookup)
6182e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
6192e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
6202e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
6212e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
6222e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
6232e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6252e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
62614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor      SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
6272e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
6282e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6302dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
6312e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    TypeLocBuilder TLB;
6322e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (isa<InjectedClassNameType>(T)) {
6332e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      InjectedClassNameTypeLoc InjectedTL
6342e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<InjectedClassNameTypeLoc>(T);
6352e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      InjectedTL.setNameLoc(IdentifierLoc);
636bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<RecordType>(T)) {
6372e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
6382e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      RecordTL.setNameLoc(IdentifierLoc);
639bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<TypedefType>(T)) {
6402e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
6412e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TypedefTL.setNameLoc(IdentifierLoc);
642bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<EnumType>(T)) {
6432e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
6442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      EnumTL.setNameLoc(IdentifierLoc);
645bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<TemplateTypeParmType>(T)) {
6462e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TemplateTypeParmTypeLoc TemplateTypeTL
6472e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<TemplateTypeParmTypeLoc>(T);
6482e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TemplateTypeTL.setNameLoc(IdentifierLoc);
649bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<UnresolvedUsingType>(T)) {
6502e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      UnresolvedUsingTypeLoc UnresolvedTL
6512e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<UnresolvedUsingTypeLoc>(T);
6522e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      UnresolvedTL.setNameLoc(IdentifierLoc);
653bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<SubstTemplateTypeParmType>(T)) {
654bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      SubstTemplateTypeParmTypeLoc TL
655bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor        = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
656bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      TL.setNameLoc(IdentifierLoc);
657bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<SubstTemplateTypeParmPackType>(T)) {
658bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      SubstTemplateTypeParmPackTypeLoc TL
659bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor        = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
660bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      TL.setNameLoc(IdentifierLoc);
661bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else {
662bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
6632e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
6642e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
66595aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith    if (T->isEnumeralType())
66695aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith      Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
66795aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith
6682e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
6692e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor              CCLoc);
6702e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
6712dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  // Otherwise, we have an error case.  If we don't want diagnostics, just
67446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  // return an error now.
67546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  if (ErrorRecoveryLookup)
6762e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
67746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
6783d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // If we didn't find anything during our lookup, try again with
6793d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // ordinary name lookup, which can help us produce better error
6803d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // messages.
6811bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (Found.empty()) {
682a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    Found.clear(LookupOrdinaryName);
683a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupName(Found, S);
684f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  }
6851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
686dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // In Microsoft mode, if we are within a templated function and we can't
687dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // resolve Identifier, then extend the SS with Identifier. This will have
688dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // the effect of resolving Identifier during template instantiation.
689dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // The goal is to be able to resolve a function call whose
690dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // nested-name-specifier is located inside a dependent base class.
691dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // Example:
692dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //
693dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // class C {
694dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // public:
695dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //    static void foo2() {  }
696dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // };
697dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // template <class T> class A { public: typedef C D; };
698dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //
699dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // template <class T> class B : public A<T> {
700dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // public:
701dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //   void foo() { D::foo2(); }
702dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // };
703651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (getLangOpts().MSVCCompat) {
704dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
705dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
706dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet      SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
707dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet      return false;
708dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    }
709dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  }
710dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet
711651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!Found.empty()) {
712651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
713651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(IdentifierLoc, diag::err_expected_class_or_namespace)
714651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << QualType(TD->getTypeForDecl(), 0) << getLangOpts().CPlusPlus;
715651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    else {
716651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(IdentifierLoc, diag::err_expected_class_or_namespace)
717651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << &Identifier << getLangOpts().CPlusPlus;
718651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
719ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        Diag(ND->getLocation(), diag::note_entity_declared_at) << &Identifier;
720651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
721651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else if (SS.isSet())
722651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(IdentifierLoc, diag::err_no_member) << &Identifier << LookupCtx
723651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                             << SS.getRange();
7243d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  else
725651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(IdentifierLoc, diag::err_undeclared_var_use) << &Identifier;
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7272e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return true;
7283d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
7293d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
7302e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
7312e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       IdentifierInfo &Identifier,
7322e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation IdentifierLoc,
7332e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
7342e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       ParsedType ObjectType,
7352e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool EnteringContext,
7366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       CXXScopeSpec &SS,
7376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool ErrorRecoveryLookup,
7386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool *IsCorrectedToColon) {
7392e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
7402e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
7416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
7422e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
7432e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     GetTypeFromParser(ObjectType),
7442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     EnteringContext, SS,
7456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     /*ScopeLookupResult=*/nullptr, false,
7466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     IsCorrectedToColon);
74746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner}
74846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
74942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikiebool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
75042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               const DeclSpec &DS,
75142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               SourceLocation ColonColonLoc) {
75242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
75342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    return true;
75442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
75542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
75642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
75742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
75842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (!T->isDependentType() && !T->getAs<TagType>()) {
759651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
7604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      << T << getLangOpts().CPlusPlus;
76142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    return true;
76242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
76342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
76442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  TypeLocBuilder TLB;
76542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
76642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
76742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
76842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie            ColonColonLoc);
76942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  return false;
77042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie}
77142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
77246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// IsInvalidUnlessNestedName - This method is used for error recovery
77346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// purposes to determine whether the specified identifier is only valid as
77446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// a nested name specifier, for example a namespace name.  It is
77546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// conservatively correct to always return false from this method.
77646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner///
77746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
7789ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
7792e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     IdentifierInfo &Identifier,
7802e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     SourceLocation IdentifierLoc,
7812e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     SourceLocation ColonLoc,
7822e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     ParsedType ObjectType,
78346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner                                     bool EnteringContext) {
7842e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
7852e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
7866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
7872e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
7882e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      GetTypeFromParser(ObjectType),
7892e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      EnteringContext, SS,
7906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      /*ScopeLookupResult=*/nullptr, true);
791c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor}
792c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
7932e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
794e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       CXXScopeSpec &SS,
795e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       SourceLocation TemplateKWLoc,
796aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       TemplateTy Template,
797aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation TemplateNameLoc,
798aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation LAngleLoc,
799aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       ASTTemplateArgsPtr TemplateArgsIn,
800aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation RAngleLoc,
8012e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
802aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       bool EnteringContext) {
8032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
8042e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
8052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
806aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // Translate the parser's template argument list in our AST format.
807aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
808aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  translateTemplateArguments(TemplateArgsIn, TemplateArgs);
809aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
810651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
811651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (DTN && DTN->isIdentifier()) {
812aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // Handle a dependent template specialization for which we cannot resolve
813aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // the template name.
814c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith    assert(DTN->getQualifier() == SS.getScopeRep());
815aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
81694fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor                                                          DTN->getQualifier(),
81794fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor                                                          DTN->getIdentifier(),
818aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                                TemplateArgs);
819aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
820aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // Create source-location information for this type.
821aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    TypeLocBuilder Builder;
82255d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    DependentTemplateSpecializationTypeLoc SpecTL
823aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
82455d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setElaboratedKeywordLoc(SourceLocation());
82555d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
82666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara    SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
82755d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setTemplateNameLoc(TemplateNameLoc);
828aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setLAngleLoc(LAngleLoc);
829aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setRAngleLoc(RAngleLoc);
830aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
831aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
832aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
833e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
834aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor              CCLoc);
835aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    return false;
836aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  }
837651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
838651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  TemplateDecl *TD = Template.get().getAsTemplateDecl();
839651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Template.get().getAsOverloadedTemplate() || DTN ||
840651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
8416cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    SourceRange R(TemplateNameLoc, RAngleLoc);
8426cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    if (SS.getRange().isValid())
8436cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      R.setBegin(SS.getRange().getBegin());
844651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
8456cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
846651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
8476cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    NoteAllFoundTemplates(Template.get());
8486cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    return true;
8496cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor  }
850651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
851aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // We were able to resolve the template name to an actual template.
852aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // Build an appropriate nested-name-specifier.
853aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
854aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                   TemplateArgs);
8552e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (T.isNull())
8562e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
8572e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
8583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Alias template specializations can produce types which are not valid
8593e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // nested name specifiers.
8603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!T->isDependentType() && !T->getAs<TagType>()) {
8613e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
8623e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    NoteAllFoundTemplates(Template.get());
8633e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return true;
8643e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
865aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
86655d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  // Provide source-location information for the template specialization type.
867aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  TypeLocBuilder Builder;
86855d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TemplateSpecializationTypeLoc SpecTL
869aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    = Builder.push<TemplateSpecializationTypeLoc>(T);
87055d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
87155d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  SpecTL.setTemplateNameLoc(TemplateNameLoc);
872aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  SpecTL.setLAngleLoc(LAngleLoc);
873aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  SpecTL.setRAngleLoc(RAngleLoc);
874aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
875aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
876aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
877aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
878e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
879aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor            CCLoc);
8802e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return false;
88139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor}
88239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
883c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregornamespace {
884c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  /// \brief A structure that stores a nested-name-specifier annotation,
885c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  /// including both the nested-name-specifier
886c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  struct NestedNameSpecifierAnnotation {
887c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    NestedNameSpecifier *NNS;
888c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  };
889c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
890c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
891c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregorvoid *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
892c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  if (SS.isEmpty() || SS.isInvalid())
8936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
8946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
895c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
896c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                        SS.location_size()),
897c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                               llvm::alignOf<NestedNameSpecifierAnnotation>());
898c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  NestedNameSpecifierAnnotation *Annotation
899c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    = new (Mem) NestedNameSpecifierAnnotation;
900c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Annotation->NNS = SS.getScopeRep();
901c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  memcpy(Annotation + 1, SS.location_data(), SS.location_size());
902c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  return Annotation;
903c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
904c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
905c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregorvoid Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
906c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                SourceRange AnnotationRange,
907c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                CXXScopeSpec &SS) {
908c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  if (!AnnotationPtr) {
909c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    SS.SetInvalid(AnnotationRange);
910c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    return;
911c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  }
912c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
913c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  NestedNameSpecifierAnnotation *Annotation
914c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
915c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
916c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
917c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
918e7e278bce2301990107cef3f873cbbf7da94469aJohn McCallbool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
919e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
920e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
921c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith  NestedNameSpecifier *Qualifier = SS.getScopeRep();
922e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
923e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // There are only two places a well-formed program may qualify a
924e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // declarator: first, when defining a namespace or class member
925e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // out-of-line, and second, when naming an explicitly-qualified
926e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // friend function.  The latter case is governed by
927e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // C++03 [basic.lookup.unqual]p10:
928e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   In a friend declaration naming a member function, a name used
929e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   in the function declarator and not part of a template-argument
930e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   in a template-id is first looked up in the scope of the member
931e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   function's class. If it is not found, or if the name is part of
932e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   a template-argument in a template-id, the look up is as
933e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   described for unqualified names in the definition of the class
934e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   granting friendship.
935e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // i.e. we don't push a scope unless it's a class member.
936e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
937e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  switch (Qualifier->getKind()) {
938e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Global:
939e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Namespace:
94014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
941e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // These are always namespace scopes.  We never want to enter a
942e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // namespace scope from anything but a file context.
9437a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return CurContext->getRedeclContext()->isFileContext();
944e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
945e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Identifier:
946e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::TypeSpec:
947e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::TypeSpecWithTemplate:
948e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // These are never namespace scopes.
949e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    return true;
950e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  }
951e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
9527530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
953e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall}
954e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
9553d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
9563d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// scope or nested-name-specifier) is parsed, part of a declarator-id.
9573d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// After this method is called, according to [C++ 3.4.3p3], names should be
9583d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// looked up in the declarator-id's scope, until the declarator is parsed and
9593d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXExitDeclaratorScope is called.
9603d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// The 'SS' should be a non-empty valid CXXScopeSpec.
9619ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
9623d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
9637a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9647a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (SS.isInvalid()) return true;
9657a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9667a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  DeclContext *DC = computeDeclContext(SS, true);
9677a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (!DC) return true;
9687a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9697a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // Before we enter a declarator's context, we need to make sure that
9707a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // it is a complete declaration context.
97177bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall  if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
9727a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall    return true;
9737a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9747a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  EnterDeclaratorContext(S, DC);
97531f17ecbef57b5679c017c375db330546b7b5145John McCall
97631f17ecbef57b5679c017c375db330546b7b5145John McCall  // Rebuild the nested name specifier for the new scope.
97731f17ecbef57b5679c017c375db330546b7b5145John McCall  if (DC->isDependentContext())
97831f17ecbef57b5679c017c375db330546b7b5145John McCall    RebuildNestedNameSpecifierInCurrentInstantiation(SS);
97931f17ecbef57b5679c017c375db330546b7b5145John McCall
9807dfd0fb08300b60a9657748bda7d8b3ceb07babeDouglas Gregor  return false;
9813d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
9823d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
9833d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
9843d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
9853d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
9863d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// Used to indicate that names should revert to being looked up in the
9873d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// defining scope.
9883d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetvoid Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
9893d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
990dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor  if (SS.isInvalid())
991dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    return;
9927a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
9937a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall         "exiting declarator scope we never really entered");
9947a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  ExitDeclaratorContext(S);
9953d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
996