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();
151176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
152176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case NestedNameSpecifier::Super:
153176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return NNS->getAsRecordDecl();
154ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  }
155ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
1567530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
157ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor}
158ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor
1595953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregorbool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
1605953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  if (!SS.isSet() || SS.isInvalid())
1615953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    return false;
1625953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
163c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith  return SS.getScopeRep()->isDependent();
1645953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor}
1655953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
16642af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// \brief If the given nested name specifier refers to the current
16742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// instantiation, return the declaration that corresponds to that
16842af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// current instantiation (C++0x [temp.dep.type]p1).
16942af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor///
17042af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// \param NNS a dependent nested name specifier.
17142af25f865a82022a04bedeb483ac251c4412e29Douglas GregorCXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
1724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "Only callable in C++");
17342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
17442af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
175f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor  if (!NNS->getAsType())
1766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1781560def1f796c0e5db6026fe366759623c9f13c2Douglas Gregor  QualType T = QualType(NNS->getAsType(), 0);
179d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  return ::getCurrentInstantiationOf(T, CurContext);
18042af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor}
18142af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
1824fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// \brief Require that the context specified by SS be complete.
1834fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor///
1844fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// If SS refers to a type, this routine checks whether the type is
1854fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// complete enough (or can be made complete enough) for name lookup
1864fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// into the DeclContext. A type that is not yet completed can be
1874fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// considered "complete enough" if it is a class/struct/union/enum
1884fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// that is currently being defined. Or, if we have a type that names
1894fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// a class template specialization that is not a complete type, we
1904fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// will attempt to instantiate that class template.
19177bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCallbool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
19277bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall                                      DeclContext *DC) {
1936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(DC && "given null context");
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
195f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  TagDecl *tag = dyn_cast<TagDecl>(DC);
1969dc71d2fddcd283e07d45f3894c8559e2f7dd9a7John McCall
197f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // If this is a dependent type, then we consider it complete.
198f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (!tag || tag->isDependentContext())
199f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
200f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
201f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // If we're currently defining this type, then lookup into the
202f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // type is okay: don't complain that it isn't complete yet.
203f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  QualType type = Context.getTypeDeclType(tag);
204f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  const TagType *tagType = type->getAs<TagType>();
205f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (tagType && tagType->isBeingDefined())
206f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
207f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
208f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SourceLocation loc = SS.getLastQualifierNameLoc();
209f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (loc.isInvalid()) loc = SS.getRange().getBegin();
210f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
211f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // The type must be complete.
212d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
213d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                          SS.getRange())) {
214f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    SS.SetInvalid(SS.getRange());
215f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return true;
2164fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor  }
2174fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor
218f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // Fixed enum types are complete, but they aren't valid as scopes
219f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // until we see a definition, so awkwardly pull out this special
220f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // case.
2213ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  // FIXME: The definition might not be visible; complain if it is not.
222f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
223f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (!enumType || enumType->getDecl()->isCompleteDefinition())
224f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
225f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
226f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // Try to instantiate the definition, if this is a specialization of an
227f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // enumeration temploid.
228f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  EnumDecl *ED = enumType->getDecl();
229f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
230f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
2311af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
2321af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
2331af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith                          TSK_ImplicitInstantiation)) {
2341af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith        SS.SetInvalid(SS.getRange());
2351af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith        return true;
2361af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      }
2371af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      return false;
2381af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    }
239f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  }
240f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
241f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Diag(loc, diag::err_incomplete_nested_name_spec)
242f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    << type << SS.getRange();
243f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SS.SetInvalid(SS.getRange());
244f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return true;
2454fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor}
2463d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
247176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc,
2482e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                        CXXScopeSpec &SS) {
2492e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  SS.MakeGlobal(Context, CCLoc);
2502e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return false;
2513d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
2523d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
253176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc,
254176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                    SourceLocation ColonColonLoc,
255176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                    CXXScopeSpec &SS) {
256176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  CXXRecordDecl *RD = nullptr;
257176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  for (Scope *S = getCurScope(); S; S = S->getParent()) {
258176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (S->isFunctionScope()) {
259176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(S->getEntity()))
260176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        RD = MD->getParent();
261176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      break;
262176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
263176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (S->isClassScope()) {
264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      RD = cast<CXXRecordDecl>(S->getEntity());
265176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      break;
266176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
267176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
268176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
269176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (!RD) {
270176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(SuperLoc, diag::err_invalid_super_scope);
271176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return true;
272176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  } else if (RD->isLambda()) {
273176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(SuperLoc, diag::err_super_in_lambda_unsupported);
274176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return true;
275176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  } else if (RD->getNumBases() == 0) {
276176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Diag(SuperLoc, diag::err_no_base_classes) << RD->getName();
277176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return true;
278176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
279176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
280176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  SS.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
281176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  return false;
282176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines}
283176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2842dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \brief Determines whether the given declaration is an valid acceptable
2852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// result for name lookup of a nested-name-specifier.
2860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// \param SD Declaration checked for nested-name-specifier.
2870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// \param IsExtension If not null and the declaration is accepted as an
2880e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// extension, the pointed variable is assigned true.
2890e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesbool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD,
2900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                           bool *IsExtension) {
2912dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (!SD)
2922dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return false;
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2942dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Namespace and namespace aliases are fine.
2952dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
2962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (!isa<TypeDecl>(SD))
2992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return false;
3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3016b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith  // Determine whether we have a class (or, in C++11, an enum) or
3022dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // a typedef thereof. If so, build the nested-name-specifier.
3032dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
3042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (T->isDependentType())
3052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
3060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
3070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (TD->getUnderlyingType()->isRecordType())
3082dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      return true;
3090e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (TD->getUnderlyingType()->isEnumeralType()) {
3100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Context.getLangOpts().CPlusPlus11)
3110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        return true;
3120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (IsExtension)
3130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        *IsExtension = true;
3140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
3150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  } else if (isa<RecordDecl>(SD)) {
3162dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
3170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  } else if (isa<EnumDecl>(SD)) {
3180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (Context.getLangOpts().CPlusPlus11)
3190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return true;
3200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (IsExtension)
3210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      *IsExtension = true;
3220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
3232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
3242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  return false;
3252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor}
3262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
327c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// \brief If the given nested-name-specifier begins with a bare identifier
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// (e.g., Base::), perform name lookup for that identifier as a
329c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// nested-name-specifier within the given scope, and return the result of that
330c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// name lookup.
331c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas GregorNamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
332c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (!S || !NNS)
3336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
335c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  while (NNS->getPrefix())
336c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    NNS = NNS->getPrefix();
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
338c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (NNS->getKind() != NestedNameSpecifier::Identifier)
3396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
342a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall                     LookupNestedNameSpecifierName);
343a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupName(Found, S);
344c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
345c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
3461bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (!Found.isSingleResult())
3476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3481bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall
3491bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  NamedDecl *Result = Found.getFoundDecl();
350edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor  if (isAcceptableNestedNameSpecifier(Result))
351c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    return Result;
3521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
354c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor}
355c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
3569ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
35777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                        SourceLocation IdLoc,
35877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                        IdentifierInfo &II,
359b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectTypePtr) {
36077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
36177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
36277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
36377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  // Determine where to perform name lookup
3646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DeclContext *LookupCtx = nullptr;
36577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  bool isDependent = false;
36677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (!ObjectType.isNull()) {
36777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // This nested-name-specifier occurs in a member access expression, e.g.,
36877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // x->B::f, and we are looking into the type of the object.
36977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
37077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupCtx = computeDeclContext(ObjectType);
37177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    isDependent = ObjectType->isDependentType();
37277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else if (SS.isSet()) {
37377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // This nested-name-specifier occurs after another nested-name-specifier,
37477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // so long into the context associated with the prior nested-name-specifier.
37577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupCtx = computeDeclContext(SS, false);
37677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    isDependent = isDependentScopeSpecifier(SS);
37777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    Found.setContextRange(SS.getRange());
37877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
37977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
38077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (LookupCtx) {
38177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // Perform "qualified" name lookup into the declaration context we
38277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // computed, which is either the type of the base of a member access
38377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // expression or the declaration context associated with a prior
38477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // nested-name-specifier.
38577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
38677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // The declaration context must be complete.
38777bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall    if (!LookupCtx->isDependentContext() &&
38877bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall        RequireCompleteDeclContext(SS, LookupCtx))
38977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      return false;
39077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
39177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupQualifiedName(Found, LookupCtx);
39277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else if (isDependent) {
39377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    return false;
39477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else {
39577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupName(Found, S);
39677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
39777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  Found.suppressDiagnostics();
39877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
39977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
40077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
40177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
40277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  return false;
40377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor}
40477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
4053b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrainnamespace {
4063b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
4073b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain// Callback to only accept typo corrections that can be a valid C++ member
4083b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain// intializer: either a non-static field member or a base class.
4093b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrainclass NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
4103b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain public:
4113b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
4123b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain      : SRef(SRef) {}
4133b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
414651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ValidateCandidate(const TypoCorrection &candidate) override {
4153b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain    return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
4163b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  }
4173b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
4183b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain private:
4193b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  Sema &SRef;
4203b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain};
4213b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
4223b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain}
4233b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
424c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// \brief Build a new nested-name-specifier for "identifier::", as described
425c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// by ActOnCXXNestedNameSpecifier.
426c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor///
4276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param S Scope in which the nested-name-specifier occurs.
4286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param Identifier Identifier in the sequence "identifier" "::".
4296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param IdentifierLoc Location of the \p Identifier.
4306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param CCLoc Location of "::" following Identifier.
4316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param ObjectType Type of postfix expression if the nested-name-specifier
4326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        occurs in construct like: <tt>ptr->nns::f</tt>.
4336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param EnteringContext If true, enter the context specified by the
4346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        nested-name-specifier.
4356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param SS Optional nested name specifier preceding the identifier.
4366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param ScopeLookupResult Provides the result of name lookup within the
4376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        scope of the nested-name-specifier that was computed at template
4386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        definition time.
4396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param ErrorRecoveryLookup Specifies if the method is called to improve
4406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        error recovery and what kind of recovery is performed.
4416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param IsCorrectedToColon If not null, suggestion of replace '::' -> ':'
4426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        are allowed.  The bool value pointed by this parameter is set to
4436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///       'true' if the identifier is treated as if it was followed by ':',
4446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        not '::'.
4456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///
4466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
447c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// that it contains an extra parameter \p ScopeLookupResult, which provides
448c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// the result of name lookup within the scope of the nested-name-specifier
449a6e51993362fcd53c13c2fa30a288d6fcbce4de6Douglas Gregor/// that was computed at template definition time.
45046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner///
45146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// If ErrorRecoveryLookup is true, then this call is used to improve error
45246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// recovery.  This means that it should not emit diagnostics, it should
4532e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// just return true on failure.  It also means it should only return a valid
45446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// scope if it *knows* that the result is correct.  It should not return in a
4552e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// dependent context, for example. Nor will it extend \p SS with the scope
4562e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// specifier.
4572e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::BuildCXXNestedNameSpecifier(Scope *S,
4582e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       IdentifierInfo &Identifier,
4592e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation IdentifierLoc,
4602e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
4612e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       QualType ObjectType,
4622e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool EnteringContext,
4632e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       CXXScopeSpec &SS,
4642e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       NamedDecl *ScopeLookupResult,
4656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool ErrorRecoveryLookup,
4666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool *IsCorrectedToColon) {
4672e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  LookupResult Found(*this, &Identifier, IdentifierLoc,
4682e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                     LookupNestedNameSpecifierName);
469a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall
4702dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Determine where to perform name lookup
4716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DeclContext *LookupCtx = nullptr;
4722dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  bool isDependent = false;
4736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (IsCorrectedToColon)
4746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    *IsCorrectedToColon = false;
475c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (!ObjectType.isNull()) {
4762dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // This nested-name-specifier occurs in a member access expression, e.g.,
4772dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // x->B::f, and we are looking into the type of the object.
4782dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
4792dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    LookupCtx = computeDeclContext(ObjectType);
4802dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    isDependent = ObjectType->isDependentType();
4812dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  } else if (SS.isSet()) {
4822dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // This nested-name-specifier occurs after another nested-name-specifier,
4833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // so look into the context associated with the prior nested-name-specifier.
4842dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    LookupCtx = computeDeclContext(SS, EnteringContext);
4852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    isDependent = isDependentScopeSpecifier(SS);
486a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    Found.setContextRange(SS.getRange());
4872dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  bool ObjectTypeSearchedInScope = false;
4902dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (LookupCtx) {
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Perform "qualified" name lookup into the declaration context we
4922dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // computed, which is either the type of the base of a member access
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // expression or the declaration context associated with a prior
4942dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // nested-name-specifier.
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // The declaration context must be complete.
49777bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall    if (!LookupCtx->isDependentContext() &&
49877bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall        RequireCompleteDeclContext(SS, LookupCtx))
4992e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
501a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupQualifiedName(Found, LookupCtx);
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    if (!ObjectType.isNull() && Found.empty()) {
5042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p4:
5052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the id-expression in a class member access is a qualified-id of
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the form
5072dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
5082dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //        class-name-or-namespace-name::...
5092dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the class-name-or-namespace-name following the . or -> operator is
5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   looked up both in the context of the entire postfix-expression and in
5122dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the scope of the class of the object expression. If the name is found
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   only in the scope of the class of the object expression, the name
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   shall refer to a class-name. If the name is found only in the
5152dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   context of the entire postfix-expression, the name shall refer to a
5162dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name. [...]
5172dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
5182dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // Qualified name lookup into a class will not find a namespace-name,
519714c992099b3b9442759f29038bb3f2c5a59a23dDouglas Gregor      // so we do not need to diagnose that case specifically. However,
5202dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // this qualified name lookup may find nothing. In that case, perform
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // unqualified name lookup in the given scope (if available) or
522c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // reconstruct the result from when name lookup was performed at template
523c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // definition time.
524c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      if (S)
525a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall        LookupName(Found, S);
526f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      else if (ScopeLookupResult)
527f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        Found.addDecl(ScopeLookupResult);
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5292dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      ObjectTypeSearchedInScope = true;
5302dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
531ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  } else if (!isDependent) {
532ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor    // Perform unqualified name lookup in the current scope.
533ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor    LookupName(Found, S);
534ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  }
535ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor
536a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (Found.isAmbiguous())
537a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    return true;
538a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
539ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  // If we performed lookup into a dependent context and did not find anything,
540ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  // that's fine: just build a dependent nested-name-specifier.
541ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  if (Found.empty() && isDependent &&
542ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor      !(LookupCtx && LookupCtx->isRecord() &&
543ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor        (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
544ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor         !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
54546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // Don't speculate if we're just trying to improve error recovery.
54646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    if (ErrorRecoveryLookup)
5472e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
5486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // We were not able to compute the declaration context for a dependent
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // base object type or prior nested-name-specifier, so this
5512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // nested-name-specifier refers to an unknown specialization. Just build
5522dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // a dependent nested-name-specifier.
5532e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
5542e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
5556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Found.empty() && !ErrorRecoveryLookup) {
5586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // If identifier is not found as class-name-or-namespace-name, but is found
5596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // as other entity, don't look for typos.
5606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
5616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (LookupCtx)
5626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      LookupQualifiedName(R, LookupCtx);
5636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    else if (S && !isDependent)
5646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      LookupName(R, S);
5656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!R.empty()) {
566a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      // Don't diagnose problems with this speculative lookup.
567a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      R.suppressDiagnostics();
5686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // The identifier is found in ordinary lookup. If correction to colon is
5696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // allowed, suggest replacement to ':'.
5706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (IsCorrectedToColon) {
5716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        *IsCorrectedToColon = true;
5726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Diag(CCLoc, diag::err_nested_name_spec_is_not_class)
5736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            << &Identifier << getLangOpts().CPlusPlus
5746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            << FixItHint::CreateReplacement(CCLoc, ":");
5756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
5766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Diag(ND->getLocation(), diag::note_declared_at);
5776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return true;
5786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
5796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // Replacement '::' -> ':' is not allowed, just issue respective error.
5806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Diag(R.getNameLoc(), diag::err_expected_class_or_namespace)
5816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          << &Identifier << getLangOpts().CPlusPlus;
5826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
583c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        Diag(ND->getLocation(), diag::note_entity_declared_at) << &Identifier;
5846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return true;
5856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
5866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
588651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
589175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    // We haven't found anything, and we're not recovering from a
590175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    // different kind of error, so look for typos.
591175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    DeclarationName Name = Found.getLookupName();
592d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    Found.clear();
593176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (TypoCorrection Corrected = CorrectTypo(
594176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines            Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
595176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines            llvm::make_unique<NestedNameSpecifierValidatorCCC>(*this),
596176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines            CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
5972d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith      if (LookupCtx) {
5982d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith        bool DroppedSpecifier =
5992d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith            Corrected.WillReplaceSpecifier() &&
6002d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith            Name.getAsString() == Corrected.getAsString(getLangOpts());
6019a5d6bb0c26215adddc5ce00f21d863160cbd0b7Bill Wendling        if (DroppedSpecifier)
6029a5d6bb0c26215adddc5ce00f21d863160cbd0b7Bill Wendling          SS.clear();
6032d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith        diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
6042d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith                                  << Name << LookupCtx << DroppedSpecifier
6052d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith                                  << SS.getRange());
6062d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith      } else
6072d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith        diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
6082d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith                                  << Name);
6092d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith
6102d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith      if (NamedDecl *ND = Corrected.getCorrectionDecl())
611d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor        Found.addDecl(ND);
612d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      Found.setLookupName(Corrected.getCorrection());
61312eb5d6aa882eb247a6c22225b625eee04217105Douglas Gregor    } else {
6142e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      Found.setLookupName(&Identifier);
61512eb5d6aa882eb247a6c22225b625eee04217105Douglas Gregor    }
616175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor  }
617175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor
6181bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  NamedDecl *SD = Found.getAsSingle<NamedDecl>();
6190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool IsExtension = false;
6200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension);
6210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (!AcceptSpec && IsExtension) {
6220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    AcceptSpec = true;
6230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Diag(IdentifierLoc, diag::ext_nested_name_spec_is_enum);
6240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
6250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (AcceptSpec) {
62605e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor    if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
62780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        !getLangOpts().CPlusPlus11) {
62805e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      // C++03 [basic.lookup.classref]p4:
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If the name is found in both contexts, the
6302dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name-or-namespace-name shall refer to the same entity.
6312dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
6322dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // We already found the name in the scope of the object. Now, look
6332dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // into the current scope (the scope of the postfix-expression) to
634c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // see if we can find the same name there. As above, if there is no
635c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // scope, reconstruct the result from the template instantiation itself.
63605e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      //
63705e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      // Note that C++11 does *not* perform this redundant lookup.
638f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      NamedDecl *OuterDecl;
639f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      if (S) {
6402e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
6412e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                LookupNestedNameSpecifierName);
642a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall        LookupName(FoundOuter, S);
6431bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall        OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
644f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      } else
645f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        OuterDecl = ScopeLookupResult;
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
647edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor      if (isAcceptableNestedNameSpecifier(OuterDecl) &&
6482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor          OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
6492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor          (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
6502dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor           !Context.hasSameType(
651c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor                            Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
6522dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                               Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
6536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (ErrorRecoveryLookup)
6546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return true;
6552e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
6562e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(IdentifierLoc,
6572e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor              diag::err_nested_name_member_ref_lookup_ambiguous)
6582e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           << &Identifier;
6592e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
6602e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           << ObjectType;
6612e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
6622e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
6632e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         // Fall through so that we'll pick the name we found in the object
6642e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         // type, since that's probably what the user wanted anyway.
6652e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor       }
6662dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
668176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
669176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
670176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
6716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // If we're just performing this lookup for error-recovery purposes,
6722e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    // don't extend the nested-name-specifier. Just return now.
6732e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (ErrorRecoveryLookup)
6742e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
675176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
676176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // The use of a nested name specifier may trigger deprecation warnings.
677176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    DiagnoseUseOfDecl(SD, CCLoc);
678176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
6792e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
6802e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
6812e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
6822e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
6832e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6852e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
68614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor      SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
6872e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
6882e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6902dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
6912e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    TypeLocBuilder TLB;
6922e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (isa<InjectedClassNameType>(T)) {
6932e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      InjectedClassNameTypeLoc InjectedTL
6942e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<InjectedClassNameTypeLoc>(T);
6952e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      InjectedTL.setNameLoc(IdentifierLoc);
696bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<RecordType>(T)) {
6972e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
6982e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      RecordTL.setNameLoc(IdentifierLoc);
699bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<TypedefType>(T)) {
7002e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
7012e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TypedefTL.setNameLoc(IdentifierLoc);
702bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<EnumType>(T)) {
7032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
7042e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      EnumTL.setNameLoc(IdentifierLoc);
705bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<TemplateTypeParmType>(T)) {
7062e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TemplateTypeParmTypeLoc TemplateTypeTL
7072e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<TemplateTypeParmTypeLoc>(T);
7082e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TemplateTypeTL.setNameLoc(IdentifierLoc);
709bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<UnresolvedUsingType>(T)) {
7102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      UnresolvedUsingTypeLoc UnresolvedTL
7112e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<UnresolvedUsingTypeLoc>(T);
7122e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      UnresolvedTL.setNameLoc(IdentifierLoc);
713bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<SubstTemplateTypeParmType>(T)) {
714bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      SubstTemplateTypeParmTypeLoc TL
715bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor        = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
716bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      TL.setNameLoc(IdentifierLoc);
717bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<SubstTemplateTypeParmPackType>(T)) {
718bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      SubstTemplateTypeParmPackTypeLoc TL
719bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor        = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
720bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      TL.setNameLoc(IdentifierLoc);
721bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else {
722bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
7232e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
7242e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
72595aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith    if (T->isEnumeralType())
72695aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith      Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
72795aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith
7282e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
7292e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor              CCLoc);
7302e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
7312dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  // Otherwise, we have an error case.  If we don't want diagnostics, just
73446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  // return an error now.
73546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  if (ErrorRecoveryLookup)
7362e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
73746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
7383d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // If we didn't find anything during our lookup, try again with
7393d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // ordinary name lookup, which can help us produce better error
7403d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // messages.
7411bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (Found.empty()) {
742a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    Found.clear(LookupOrdinaryName);
743a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupName(Found, S);
744f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  }
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
746dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // In Microsoft mode, if we are within a templated function and we can't
747dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // resolve Identifier, then extend the SS with Identifier. This will have
748dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // the effect of resolving Identifier during template instantiation.
749dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // The goal is to be able to resolve a function call whose
750dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // nested-name-specifier is located inside a dependent base class.
751dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // Example:
752dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //
753dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // class C {
754dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // public:
755dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //    static void foo2() {  }
756dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // };
757dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // template <class T> class A { public: typedef C D; };
758dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //
759dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // template <class T> class B : public A<T> {
760dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // public:
761dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //   void foo() { D::foo2(); }
762dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // };
763651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (getLangOpts().MSVCCompat) {
764dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
765dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
766176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
767176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
768176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        Diag(IdentifierLoc, diag::ext_undeclared_unqual_id_with_dependent_base)
769176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines            << &Identifier << ContainingClass;
770176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
771176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return false;
772176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
773dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    }
774dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  }
775dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet
776651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!Found.empty()) {
777651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
778651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(IdentifierLoc, diag::err_expected_class_or_namespace)
779651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << QualType(TD->getTypeForDecl(), 0) << getLangOpts().CPlusPlus;
780651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    else {
781651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(IdentifierLoc, diag::err_expected_class_or_namespace)
782651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << &Identifier << getLangOpts().CPlusPlus;
783651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
784c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        Diag(ND->getLocation(), diag::note_entity_declared_at) << &Identifier;
785651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
786651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else if (SS.isSet())
787651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(IdentifierLoc, diag::err_no_member) << &Identifier << LookupCtx
788651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                             << SS.getRange();
7893d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  else
790651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(IdentifierLoc, diag::err_undeclared_var_use) << &Identifier;
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7922e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return true;
7933d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
7943d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
7952e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
7962e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       IdentifierInfo &Identifier,
7972e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation IdentifierLoc,
7982e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
7992e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       ParsedType ObjectType,
8002e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool EnteringContext,
8016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       CXXScopeSpec &SS,
8026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool ErrorRecoveryLookup,
8036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       bool *IsCorrectedToColon) {
8042e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
8052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
8066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
8072e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
8082e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     GetTypeFromParser(ObjectType),
8092e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     EnteringContext, SS,
8106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     /*ScopeLookupResult=*/nullptr, false,
8116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     IsCorrectedToColon);
81246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner}
81346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
81442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikiebool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
81542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               const DeclSpec &DS,
81642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               SourceLocation ColonColonLoc) {
81742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
81842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    return true;
81942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
82042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
82142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
82242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
82342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (!T->isDependentType() && !T->getAs<TagType>()) {
824651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
8254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      << T << getLangOpts().CPlusPlus;
82642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    return true;
82742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
82842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
82942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  TypeLocBuilder TLB;
83042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
83142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
83242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
83342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie            ColonColonLoc);
83442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  return false;
83542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie}
83642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
83746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// IsInvalidUnlessNestedName - This method is used for error recovery
83846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// purposes to determine whether the specified identifier is only valid as
83946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// a nested name specifier, for example a namespace name.  It is
84046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// conservatively correct to always return false from this method.
84146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner///
84246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
8439ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
8442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     IdentifierInfo &Identifier,
8452e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     SourceLocation IdentifierLoc,
8462e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     SourceLocation ColonLoc,
8472e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     ParsedType ObjectType,
84846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner                                     bool EnteringContext) {
8492e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
8502e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
8516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
8522e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
8532e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      GetTypeFromParser(ObjectType),
8542e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      EnteringContext, SS,
8556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      /*ScopeLookupResult=*/nullptr, true);
856c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor}
857c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
8582e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
859e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       CXXScopeSpec &SS,
860e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       SourceLocation TemplateKWLoc,
861aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       TemplateTy Template,
862aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation TemplateNameLoc,
863aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation LAngleLoc,
864aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       ASTTemplateArgsPtr TemplateArgsIn,
865aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation RAngleLoc,
8662e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
867aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       bool EnteringContext) {
8682e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
8692e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
8702e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
871aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // Translate the parser's template argument list in our AST format.
872aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
873aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  translateTemplateArguments(TemplateArgsIn, TemplateArgs);
874aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
875651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
876651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (DTN && DTN->isIdentifier()) {
877aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // Handle a dependent template specialization for which we cannot resolve
878aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // the template name.
879c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith    assert(DTN->getQualifier() == SS.getScopeRep());
880aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
88194fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor                                                          DTN->getQualifier(),
88294fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor                                                          DTN->getIdentifier(),
883aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                                TemplateArgs);
884aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
885aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // Create source-location information for this type.
886aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    TypeLocBuilder Builder;
88755d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    DependentTemplateSpecializationTypeLoc SpecTL
888aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
88955d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setElaboratedKeywordLoc(SourceLocation());
89055d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
89166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara    SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
89255d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setTemplateNameLoc(TemplateNameLoc);
893aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setLAngleLoc(LAngleLoc);
894aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setRAngleLoc(RAngleLoc);
895aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
896aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
897aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
898e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
899aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor              CCLoc);
900aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    return false;
901aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  }
902651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
903651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  TemplateDecl *TD = Template.get().getAsTemplateDecl();
904651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Template.get().getAsOverloadedTemplate() || DTN ||
905651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
9066cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    SourceRange R(TemplateNameLoc, RAngleLoc);
9076cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    if (SS.getRange().isValid())
9086cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      R.setBegin(SS.getRange().getBegin());
909651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
9106cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
911651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
9126cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    NoteAllFoundTemplates(Template.get());
9136cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    return true;
9146cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor  }
915651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
916aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // We were able to resolve the template name to an actual template.
917aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // Build an appropriate nested-name-specifier.
918aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
919aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                   TemplateArgs);
9202e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (T.isNull())
9212e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
9222e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
9233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Alias template specializations can produce types which are not valid
9243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // nested name specifiers.
9253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!T->isDependentType() && !T->getAs<TagType>()) {
9263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
9273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    NoteAllFoundTemplates(Template.get());
9283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return true;
9293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
930aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
93155d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  // Provide source-location information for the template specialization type.
932aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  TypeLocBuilder Builder;
93355d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TemplateSpecializationTypeLoc SpecTL
934aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    = Builder.push<TemplateSpecializationTypeLoc>(T);
93555d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
93655d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  SpecTL.setTemplateNameLoc(TemplateNameLoc);
937aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  SpecTL.setLAngleLoc(LAngleLoc);
938aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  SpecTL.setRAngleLoc(RAngleLoc);
939aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
940aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
941aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
942aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
943e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
944aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor            CCLoc);
9452e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return false;
94639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor}
94739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
948c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregornamespace {
949c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  /// \brief A structure that stores a nested-name-specifier annotation,
950c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  /// including both the nested-name-specifier
951c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  struct NestedNameSpecifierAnnotation {
952c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    NestedNameSpecifier *NNS;
953c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  };
954c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
955c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
956c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregorvoid *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
957c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  if (SS.isEmpty() || SS.isInvalid())
9586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
9596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
960c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
961c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                        SS.location_size()),
962c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                               llvm::alignOf<NestedNameSpecifierAnnotation>());
963c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  NestedNameSpecifierAnnotation *Annotation
964c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    = new (Mem) NestedNameSpecifierAnnotation;
965c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Annotation->NNS = SS.getScopeRep();
966c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  memcpy(Annotation + 1, SS.location_data(), SS.location_size());
967c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  return Annotation;
968c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
969c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
970c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregorvoid Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
971c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                SourceRange AnnotationRange,
972c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                CXXScopeSpec &SS) {
973c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  if (!AnnotationPtr) {
974c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    SS.SetInvalid(AnnotationRange);
975c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    return;
976c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  }
977c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
978c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  NestedNameSpecifierAnnotation *Annotation
979c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
980c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
981c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
982c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
983e7e278bce2301990107cef3f873cbbf7da94469aJohn McCallbool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
984e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
985e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
986c2e935f28079862b212f6b2af3057a5f203dcbc0Richard Smith  NestedNameSpecifier *Qualifier = SS.getScopeRep();
987e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
988e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // There are only two places a well-formed program may qualify a
989e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // declarator: first, when defining a namespace or class member
990e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // out-of-line, and second, when naming an explicitly-qualified
991e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // friend function.  The latter case is governed by
992e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // C++03 [basic.lookup.unqual]p10:
993e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   In a friend declaration naming a member function, a name used
994e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   in the function declarator and not part of a template-argument
995e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   in a template-id is first looked up in the scope of the member
996e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   function's class. If it is not found, or if the name is part of
997e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   a template-argument in a template-id, the look up is as
998e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   described for unqualified names in the definition of the class
999e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   granting friendship.
1000e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // i.e. we don't push a scope unless it's a class member.
1001e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
1002e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  switch (Qualifier->getKind()) {
1003e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Global:
1004e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Namespace:
100514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
1006e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // These are always namespace scopes.  We never want to enter a
1007e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // namespace scope from anything but a file context.
10087a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return CurContext->getRedeclContext()->isFileContext();
1009e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
1010e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Identifier:
1011e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::TypeSpec:
1012e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::TypeSpecWithTemplate:
1013176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case NestedNameSpecifier::Super:
1014e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // These are never namespace scopes.
1015e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    return true;
1016e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  }
1017e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
10187530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
1019e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall}
1020e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
10213d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
10223d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// scope or nested-name-specifier) is parsed, part of a declarator-id.
10233d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// After this method is called, according to [C++ 3.4.3p3], names should be
10243d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// looked up in the declarator-id's scope, until the declarator is parsed and
10253d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXExitDeclaratorScope is called.
10263d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// The 'SS' should be a non-empty valid CXXScopeSpec.
10279ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
10283d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
10297a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
10307a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (SS.isInvalid()) return true;
10317a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
10327a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  DeclContext *DC = computeDeclContext(SS, true);
10337a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (!DC) return true;
10347a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
10357a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // Before we enter a declarator's context, we need to make sure that
10367a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // it is a complete declaration context.
103777bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall  if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
10387a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall    return true;
10397a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
10407a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  EnterDeclaratorContext(S, DC);
104131f17ecbef57b5679c017c375db330546b7b5145John McCall
104231f17ecbef57b5679c017c375db330546b7b5145John McCall  // Rebuild the nested name specifier for the new scope.
104331f17ecbef57b5679c017c375db330546b7b5145John McCall  if (DC->isDependentContext())
104431f17ecbef57b5679c017c375db330546b7b5145John McCall    RebuildNestedNameSpecifierInCurrentInstantiation(SS);
104531f17ecbef57b5679c017c375db330546b7b5145John McCall
10467dfd0fb08300b60a9657748bda7d8b3ceb07babeDouglas Gregor  return false;
10473d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
10483d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
10493d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
10503d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
10513d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
10523d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// Used to indicate that names should revert to being looked up in the
10533d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// defining scope.
10543d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetvoid Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
10553d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1056dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor  if (SS.isInvalid())
1057dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    return;
10587a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
10597a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall         "exiting declarator scope we never really entered");
10607a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  ExitDeclaratorContext(S);
10613d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
1062