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"
15e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
16f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith#include "clang/Sema/Template.h"
173d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet#include "clang/AST/ASTContext.h"
1842af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor#include "clang/AST/DeclTemplate.h"
19fe85cedd58df7daed29201703cfb8806e12876d0Douglas Gregor#include "clang/AST/ExprCXX.h"
20e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor#include "clang/AST/NestedNameSpecifier.h"
21b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
2219510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
232e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor#include "TypeLocBuilder.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.
29d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregorstatic CXXRecordDecl *getCurrentInstantiationOf(QualType T,
30d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor                                                DeclContext *CurContext) {
3143d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor  if (T.isNull())
3243d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor    return 0;
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());
37d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    if (!T->isDependentType())
38d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor      return Record;
39d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor
40d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    // This may be a member of a class template or class template partial
41d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    // specialization. If it's part of the current semantic context, then it's
42d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    // an injected-class-name;
43d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
44d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor      if (CurContext->Equals(Record))
45d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor        return Record;
46d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor
47d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    return 0;
48d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  } else if (isa<InjectedClassNameType>(Ty))
4931f17ecbef57b5679c017c375db330546b7b5145John McCall    return cast<InjectedClassNameType>(Ty)->getDecl();
5031f17ecbef57b5679c017c375db330546b7b5145John McCall  else
5131f17ecbef57b5679c017c375db330546b7b5145John McCall    return 0;
5243d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor}
5343d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor
542dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \brief Compute the DeclContext that is associated with the given type.
552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param T the type for which we are attempting to find a DeclContext.
572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \returns the declaration context represented by the type T,
592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// or NULL if the declaration context cannot be computed (e.g., because it is
602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// dependent and not the current instantiation).
612dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas GregorDeclContext *Sema::computeDeclContext(QualType T) {
62d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  if (!T->isDependentType())
63d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor    if (const TagType *Tag = T->getAs<TagType>())
64d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor      return Tag->getDecl();
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  return ::getCurrentInstantiationOf(T, CurContext);
672dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor}
682dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
69e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor/// \brief Compute the DeclContext that is associated with the given
70e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor/// scope specifier.
71f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor///
72f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// \param SS the C++ scope specifier as it appears in the source
73f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor///
74f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// \param EnteringContext when true, we will be entering the context of
75f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// this scope specifier, so we can retrieve the declaration context of a
76f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// class template or class template partial specialization even if it is
77f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// not the current instantiation.
78f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor///
79f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// \returns the declaration context represented by the scope specifier @p SS,
80f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// or NULL if the declaration context cannot be computed (e.g., because it is
81f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor/// dependent and not the current instantiation).
82f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas GregorDeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
83f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor                                      bool EnteringContext) {
84e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (!SS.isSet() || SS.isInvalid())
85ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor    return 0;
86ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS
883507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
8942af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  if (NNS->isDependent()) {
9042af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    // If this nested-name-specifier refers to the current
9142af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    // instantiation, return its DeclContext.
9242af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
9342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor      return Record;
941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor    if (EnteringContext) {
963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      const Type *NNSType = NNS->getAsType();
973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (!NNSType) {
983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith        return 0;
993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      }
1003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      // Look through type alias templates, per C++0x [temp.dep.type]p1.
1023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      NNSType = Context.getCanonicalType(NNSType);
1033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      if (const TemplateSpecializationType *SpecType
1043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith            = NNSType->getAs<TemplateSpecializationType>()) {
105495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // We are entering the context of the nested name specifier, so try to
106495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // match the nested name specifier to either a primary class template
107495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // or a class template partial specialization.
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateDecl *ClassTemplate
109f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor              = dyn_cast_or_null<ClassTemplateDecl>(
110f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor                            SpecType->getTemplateName().getAsTemplateDecl())) {
111b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          QualType ContextType
112b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor            = Context.getCanonicalType(QualType(SpecType, 0));
113b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor
114f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor          // If the type of the nested name specifier is the same as the
115f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor          // injected class name of the named class template, we're entering
116f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor          // into that class template definition.
1173cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          QualType Injected
11824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor            = ClassTemplate->getInjectedClassNameSpecialization();
119b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          if (Context.hasSameType(Injected, ContextType))
120f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor            return ClassTemplate->getTemplatedDecl();
1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // If the type of the nested name specifier is the same as the
123b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // type of one of the class template's class template partial
124b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // specializations, we're entering into the definition of that
125b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          // class template partial specialization.
126b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor          if (ClassTemplatePartialSpecializationDecl *PartialSpec
127b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                = ClassTemplate->findPartialSpecialization(ContextType))
128b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor            return PartialSpec;
129f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor        }
1303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
131495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        // The nested name specifier refers to a member of a class template.
132495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        return RecordT->getDecl();
133f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor      }
134f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor    }
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor    return 0;
13742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  }
138ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
139ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  switch (NNS->getKind()) {
140ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::Identifier:
141b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
142ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
143ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::Namespace:
144ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    return NNS->getAsNamespace();
145ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
14614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
14714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NNS->getAsNamespaceAlias()->getNamespace();
14814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
149ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::TypeSpec:
150ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
151edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    const TagType *Tag = NNS->getAsType()->getAs<TagType>();
152edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    assert(Tag && "Non-tag type in nested-name-specifier");
153edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    return Tag->getDecl();
1547530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
155ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
156ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  case NestedNameSpecifier::Global:
157ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    return Context.getTranslationUnitDecl();
158ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  }
159ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
1607530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
161ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor}
162ca5e77fefc4ac06aa787d7e777957ba6b7a03c60Douglas Gregor
1635953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregorbool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
1645953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  if (!SS.isSet() || SS.isInvalid())
1655953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    return false;
1665953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS
1683507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
169ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return NNS->isDependent();
1705953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor}
1715953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
17242af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor// \brief Determine whether this C++ scope specifier refers to an
17342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor// unknown specialization, i.e., a dependent type that is not the
17442af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor// current instantiation.
17542af25f865a82022a04bedeb483ac251c4412e29Douglas Gregorbool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) {
17642af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  if (!isDependentScopeSpecifier(SS))
17742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    return false;
17842af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS
18042af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
18142af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  return getCurrentInstantiationOf(NNS) == 0;
18242af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor}
18342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
18442af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// \brief If the given nested name specifier refers to the current
18542af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// instantiation, return the declaration that corresponds to that
18642af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// current instantiation (C++0x [temp.dep.type]p1).
18742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor///
18842af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor/// \param NNS a dependent nested name specifier.
18942af25f865a82022a04bedeb483ac251c4412e29Douglas GregorCXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
1904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "Only callable in C++");
19142af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor  assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
19242af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
193f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor  if (!NNS->getAsType())
194f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor    return 0;
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1961560def1f796c0e5db6026fe366759623c9f13c2Douglas Gregor  QualType T = QualType(NNS->getAsType(), 0);
197d9ea180032eda76a46c099a9aab99512447c326dDouglas Gregor  return ::getCurrentInstantiationOf(T, CurContext);
19842af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor}
19942af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor
2004fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// \brief Require that the context specified by SS be complete.
2014fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor///
2024fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// If SS refers to a type, this routine checks whether the type is
2034fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// complete enough (or can be made complete enough) for name lookup
2044fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// into the DeclContext. A type that is not yet completed can be
2054fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// considered "complete enough" if it is a class/struct/union/enum
2064fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// that is currently being defined. Or, if we have a type that names
2074fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// a class template specialization that is not a complete type, we
2084fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor/// will attempt to instantiate that class template.
20977bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCallbool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
21077bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall                                      DeclContext *DC) {
21177bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall  assert(DC != 0 && "given null context");
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  TagDecl *tag = dyn_cast<TagDecl>(DC);
2149dc71d2fddcd283e07d45f3894c8559e2f7dd9a7John McCall
215f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // If this is a dependent type, then we consider it complete.
216f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (!tag || tag->isDependentContext())
217f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
218f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
219f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // If we're currently defining this type, then lookup into the
220f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // type is okay: don't complain that it isn't complete yet.
221f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  QualType type = Context.getTypeDeclType(tag);
222f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  const TagType *tagType = type->getAs<TagType>();
223f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (tagType && tagType->isBeingDefined())
224f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
225f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
226f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SourceLocation loc = SS.getLastQualifierNameLoc();
227f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (loc.isInvalid()) loc = SS.getRange().getBegin();
228f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
229f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // The type must be complete.
230d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
231d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                          SS.getRange())) {
232f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    SS.SetInvalid(SS.getRange());
233f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return true;
2344fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor  }
2354fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor
236f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // Fixed enum types are complete, but they aren't valid as scopes
237f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // until we see a definition, so awkwardly pull out this special
238f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // case.
239f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
240f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (!enumType || enumType->getDecl()->isCompleteDefinition())
241f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return false;
242f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
243f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // Try to instantiate the definition, if this is a specialization of an
244f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // enumeration temploid.
245f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  EnumDecl *ED = enumType->getDecl();
246f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
247f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
2481af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
2491af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
2501af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith                          TSK_ImplicitInstantiation)) {
2511af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith        SS.SetInvalid(SS.getRange());
2521af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith        return true;
2531af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      }
2541af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      return false;
2551af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    }
256f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  }
257f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
258f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Diag(loc, diag::err_incomplete_nested_name_spec)
259f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    << type << SS.getRange();
260f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SS.SetInvalid(SS.getRange());
261f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return true;
2624fdf1faedbca40787fd277a6fbd5061fd69b2708Douglas Gregor}
2633d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
2642e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
2652e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                        CXXScopeSpec &SS) {
2662e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  SS.MakeGlobal(Context, CCLoc);
2672e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return false;
2683d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
2693d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
2702dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \brief Determines whether the given declaration is an valid acceptable
2712dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// result for name lookup of a nested-name-specifier.
272edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregorbool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) {
2732dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (!SD)
2742dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return false;
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2762dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Namespace and namespace aliases are fine.
2772dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
2782dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2802dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (!isa<TypeDecl>(SD))
2812dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return false;
2821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2836b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith  // Determine whether we have a class (or, in C++11, an enum) or
2842dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // a typedef thereof. If so, build the nested-name-specifier.
2852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
2862dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (T->isDependentType())
2872dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
288162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
2892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (TD->getUnderlyingType()->isRecordType() ||
2904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        (Context.getLangOpts().CPlusPlus0x &&
2912dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor         TD->getUnderlyingType()->isEnumeralType()))
2922dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      return true;
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (isa<RecordDecl>(SD) ||
2944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie             (Context.getLangOpts().CPlusPlus0x && isa<EnumDecl>(SD)))
2952dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    return true;
2962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
2972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  return false;
2982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor}
2992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
300c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// \brief If the given nested-name-specifier begins with a bare identifier
3011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// (e.g., Base::), perform name lookup for that identifier as a
302c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// nested-name-specifier within the given scope, and return the result of that
303c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// name lookup.
304c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas GregorNamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
305c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (!S || !NNS)
306c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    return 0;
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
308c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  while (NNS->getPrefix())
309c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    NNS = NNS->getPrefix();
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (NNS->getKind() != NestedNameSpecifier::Identifier)
312c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    return 0;
3131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
314a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
315a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall                     LookupNestedNameSpecifierName);
316a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupName(Found, S);
317c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
318c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
3191bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (!Found.isSingleResult())
3201bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall    return 0;
3211bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall
3221bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  NamedDecl *Result = Found.getFoundDecl();
323edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor  if (isAcceptableNestedNameSpecifier(Result))
324c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor    return Result;
3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
326c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  return 0;
327c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor}
328c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
3299ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
33077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                        SourceLocation IdLoc,
33177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                        IdentifierInfo &II,
332b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectTypePtr) {
33377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
33477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
33577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
33677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  // Determine where to perform name lookup
33777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  DeclContext *LookupCtx = 0;
33877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  bool isDependent = false;
33977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (!ObjectType.isNull()) {
34077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // This nested-name-specifier occurs in a member access expression, e.g.,
34177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // x->B::f, and we are looking into the type of the object.
34277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
34377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupCtx = computeDeclContext(ObjectType);
34477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    isDependent = ObjectType->isDependentType();
34577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else if (SS.isSet()) {
34677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // This nested-name-specifier occurs after another nested-name-specifier,
34777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // so long into the context associated with the prior nested-name-specifier.
34877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupCtx = computeDeclContext(SS, false);
34977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    isDependent = isDependentScopeSpecifier(SS);
35077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    Found.setContextRange(SS.getRange());
35177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
35277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
35377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (LookupCtx) {
35477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // Perform "qualified" name lookup into the declaration context we
35577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // computed, which is either the type of the base of a member access
35677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // expression or the declaration context associated with a prior
35777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // nested-name-specifier.
35877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
35977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    // The declaration context must be complete.
36077bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall    if (!LookupCtx->isDependentContext() &&
36177bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall        RequireCompleteDeclContext(SS, LookupCtx))
36277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      return false;
36377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
36477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupQualifiedName(Found, LookupCtx);
36577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else if (isDependent) {
36677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    return false;
36777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else {
36877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    LookupName(Found, S);
36977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
37077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  Found.suppressDiagnostics();
37177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
37277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
37377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
37477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
37577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  return false;
37677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor}
37777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
3783b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrainnamespace {
3793b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3803b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain// Callback to only accept typo corrections that can be a valid C++ member
3813b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain// intializer: either a non-static field member or a base class.
3823b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrainclass NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
3833b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain public:
3843b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
3853b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain      : SRef(SRef) {}
3863b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3873b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  virtual bool ValidateCandidate(const TypoCorrection &candidate) {
3883b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain    return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
3893b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  }
3903b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3913b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain private:
3923b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain  Sema &SRef;
3933b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain};
3943b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
3953b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain}
3963b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain
397c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// \brief Build a new nested-name-specifier for "identifier::", as described
398c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// by ActOnCXXNestedNameSpecifier.
399c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor///
400c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
401c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// that it contains an extra parameter \p ScopeLookupResult, which provides
402c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor/// the result of name lookup within the scope of the nested-name-specifier
403a6e51993362fcd53c13c2fa30a288d6fcbce4de6Douglas Gregor/// that was computed at template definition time.
40446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner///
40546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// If ErrorRecoveryLookup is true, then this call is used to improve error
40646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// recovery.  This means that it should not emit diagnostics, it should
4072e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// just return true on failure.  It also means it should only return a valid
40846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// scope if it *knows* that the result is correct.  It should not return in a
4092e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// dependent context, for example. Nor will it extend \p SS with the scope
4102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor/// specifier.
4112e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::BuildCXXNestedNameSpecifier(Scope *S,
4122e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       IdentifierInfo &Identifier,
4132e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation IdentifierLoc,
4142e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
4152e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       QualType ObjectType,
4162e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool EnteringContext,
4172e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       CXXScopeSpec &SS,
4182e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       NamedDecl *ScopeLookupResult,
4192e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool ErrorRecoveryLookup) {
4202e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  LookupResult Found(*this, &Identifier, IdentifierLoc,
4212e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                     LookupNestedNameSpecifierName);
422a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall
4232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Determine where to perform name lookup
4242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  DeclContext *LookupCtx = 0;
4252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  bool isDependent = false;
426c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  if (!ObjectType.isNull()) {
4272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // This nested-name-specifier occurs in a member access expression, e.g.,
4282dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // x->B::f, and we are looking into the type of the object.
4292dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
4302dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    LookupCtx = computeDeclContext(ObjectType);
4312dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    isDependent = ObjectType->isDependentType();
4322dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  } else if (SS.isSet()) {
4332dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // This nested-name-specifier occurs after another nested-name-specifier,
4343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // so look into the context associated with the prior nested-name-specifier.
4352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    LookupCtx = computeDeclContext(SS, EnteringContext);
4362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    isDependent = isDependentScopeSpecifier(SS);
437a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    Found.setContextRange(SS.getRange());
4382dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
440a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall
4412dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  bool ObjectTypeSearchedInScope = false;
4422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (LookupCtx) {
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Perform "qualified" name lookup into the declaration context we
4442dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // computed, which is either the type of the base of a member access
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // expression or the declaration context associated with a prior
4462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // nested-name-specifier.
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // The declaration context must be complete.
44977bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall    if (!LookupCtx->isDependentContext() &&
45077bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall        RequireCompleteDeclContext(SS, LookupCtx))
4512e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
453a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupQualifiedName(Found, LookupCtx);
4541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
455a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    if (!ObjectType.isNull() && Found.empty()) {
4562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p4:
4572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the id-expression in a class member access is a qualified-id of
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the form
4592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
4602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //        class-name-or-namespace-name::...
4612dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the class-name-or-namespace-name following the . or -> operator is
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   looked up both in the context of the entire postfix-expression and in
4642dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the scope of the class of the object expression. If the name is found
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   only in the scope of the class of the object expression, the name
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   shall refer to a class-name. If the name is found only in the
4672dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   context of the entire postfix-expression, the name shall refer to a
4682dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name. [...]
4692dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
4702dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // Qualified name lookup into a class will not find a namespace-name,
471714c992099b3b9442759f29038bb3f2c5a59a23dDouglas Gregor      // so we do not need to diagnose that case specifically. However,
4722dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // this qualified name lookup may find nothing. In that case, perform
4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // unqualified name lookup in the given scope (if available) or
474c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // reconstruct the result from when name lookup was performed at template
475c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // definition time.
476c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      if (S)
477a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall        LookupName(Found, S);
478f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      else if (ScopeLookupResult)
479f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        Found.addDecl(ScopeLookupResult);
4801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4812dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      ObjectTypeSearchedInScope = true;
4822dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
483ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  } else if (!isDependent) {
484ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor    // Perform unqualified name lookup in the current scope.
485ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor    LookupName(Found, S);
486ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  }
487ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor
488ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  // If we performed lookup into a dependent context and did not find anything,
489ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  // that's fine: just build a dependent nested-name-specifier.
490ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  if (Found.empty() && isDependent &&
491ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor      !(LookupCtx && LookupCtx->isRecord() &&
492ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor        (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
493ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor         !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
49446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // Don't speculate if we're just trying to improve error recovery.
49546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    if (ErrorRecoveryLookup)
4962e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
49746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
4982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // We were not able to compute the declaration context for a dependent
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // base object type or prior nested-name-specifier, so this
5002dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // nested-name-specifier refers to an unknown specialization. Just build
5012dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    // a dependent nested-name-specifier.
5022e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
5032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
504ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor  }
505ac7cbd8102a944c7e988b066fc52c03fdd536dc0Douglas Gregor
5062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // FIXME: Deal with ambiguities cleanly.
507175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor
508175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor  if (Found.empty() && !ErrorRecoveryLookup) {
509175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    // We haven't found anything, and we're not recovering from a
510175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    // different kind of error, so look for typos.
511175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor    DeclarationName Name = Found.getLookupName();
5123b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain    NestedNameSpecifierValidatorCCC Validator(*this);
513d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    TypoCorrection Corrected;
514d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    Found.clear();
515d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    if ((Corrected = CorrectTypo(Found.getLookupNameInfo(),
51616e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain                                 Found.getLookupKind(), S, &SS, Validator,
5173b4b047ff0ebaefeaed0a5f545b4fa91f18e6cdbKaelyn Uhrain                                 LookupCtx, EnteringContext))) {
5184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
5194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
520175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor      if (LookupCtx)
521175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor        Diag(Found.getNameLoc(), diag::err_no_member_suggest)
522d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor          << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
523d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor          << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
524175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor      else
525175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor        Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
526d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor          << Name << CorrectedQuotedStr
527d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor          << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
52867dd1d4df1b28973e12e0981129b2517d2033b66Douglas Gregor
529d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
530d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor        Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr;
531d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor        Found.addDecl(ND);
532d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      }
533d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      Found.setLookupName(Corrected.getCorrection());
53412eb5d6aa882eb247a6c22225b625eee04217105Douglas Gregor    } else {
5352e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      Found.setLookupName(&Identifier);
53612eb5d6aa882eb247a6c22225b625eee04217105Douglas Gregor    }
537175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor  }
538175a65686eba7c7a9cb02412136fddd2d2c56dd7Douglas Gregor
5391bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  NamedDecl *SD = Found.getAsSingle<NamedDecl>();
540edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor  if (isAcceptableNestedNameSpecifier(SD)) {
54105e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor    if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
54205e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor        !getLangOpts().CPlusPlus0x) {
54305e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      // C++03 [basic.lookup.classref]p4:
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If the name is found in both contexts, the
5452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name-or-namespace-name shall refer to the same entity.
5462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
5472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // We already found the name in the scope of the object. Now, look
5482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // into the current scope (the scope of the postfix-expression) to
549c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // see if we can find the same name there. As above, if there is no
550c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor      // scope, reconstruct the result from the template instantiation itself.
55105e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      //
55205e6076f0a02dbb73d20a3928976bcd242a13279Douglas Gregor      // Note that C++11 does *not* perform this redundant lookup.
553f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      NamedDecl *OuterDecl;
554f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      if (S) {
5552e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
5562e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                LookupNestedNameSpecifierName);
557a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall        LookupName(FoundOuter, S);
5581bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall        OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
559f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall      } else
560f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        OuterDecl = ScopeLookupResult;
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
562edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor      if (isAcceptableNestedNameSpecifier(OuterDecl) &&
5632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor          OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
5642dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor          (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
5652dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor           !Context.hasSameType(
566c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor                            Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
5672dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                               Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
5682e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         if (ErrorRecoveryLookup)
5692e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           return true;
5702e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
5712e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(IdentifierLoc,
5722e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor              diag::err_nested_name_member_ref_lookup_ambiguous)
5732e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           << &Identifier;
5742e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
5752e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor           << ObjectType;
5762e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
5772e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
5782e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         // Fall through so that we'll pick the name we found in the object
5792e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor         // type, since that's probably what the user wanted anyway.
5802e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor       }
5812dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
5821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5832e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    // If we're just performing this lookup for error-recovery purposes,
5842e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    // don't extend the nested-name-specifier. Just return now.
5852e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (ErrorRecoveryLookup)
5862e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
5872e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
5882e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
5892e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
5902e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
5912e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5932e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
59414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor      SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
5952e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return false;
5962e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
5992e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    TypeLocBuilder TLB;
6002e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (isa<InjectedClassNameType>(T)) {
6012e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      InjectedClassNameTypeLoc InjectedTL
6022e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<InjectedClassNameTypeLoc>(T);
6032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      InjectedTL.setNameLoc(IdentifierLoc);
604bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<RecordType>(T)) {
6052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
6062e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      RecordTL.setNameLoc(IdentifierLoc);
607bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<TypedefType>(T)) {
6082e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
6092e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TypedefTL.setNameLoc(IdentifierLoc);
610bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<EnumType>(T)) {
6112e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
6122e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      EnumTL.setNameLoc(IdentifierLoc);
613bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<TemplateTypeParmType>(T)) {
6142e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TemplateTypeParmTypeLoc TemplateTypeTL
6152e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<TemplateTypeParmTypeLoc>(T);
6162e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      TemplateTypeTL.setNameLoc(IdentifierLoc);
617bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<UnresolvedUsingType>(T)) {
6182e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      UnresolvedUsingTypeLoc UnresolvedTL
6192e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        = TLB.push<UnresolvedUsingTypeLoc>(T);
6202e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      UnresolvedTL.setNameLoc(IdentifierLoc);
621bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<SubstTemplateTypeParmType>(T)) {
622bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      SubstTemplateTypeParmTypeLoc TL
623bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor        = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
624bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      TL.setNameLoc(IdentifierLoc);
625bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else if (isa<SubstTemplateTypeParmPackType>(T)) {
626bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      SubstTemplateTypeParmPackTypeLoc TL
627bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor        = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
628bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      TL.setNameLoc(IdentifierLoc);
629bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor    } else {
630bd61e341d6efb9b3eaee97e48f98876af128349cDouglas Gregor      llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
6312e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    }
6322e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
63395aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith    if (T->isEnumeralType())
63495aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith      Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
63595aafb2453e1fecec8dcfd9e125cd78277f45859Richard Smith
6362e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
6372e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor              CCLoc);
6382e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
6392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  // Otherwise, we have an error case.  If we don't want diagnostics, just
64246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  // return an error now.
64346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner  if (ErrorRecoveryLookup)
6442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
64546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
6463d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // If we didn't find anything during our lookup, try again with
6473d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // ordinary name lookup, which can help us produce better error
6483d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  // messages.
6491bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (Found.empty()) {
650a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    Found.clear(LookupOrdinaryName);
651a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupName(Found, S);
652f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  }
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // In Microsoft mode, if we are within a templated function and we can't
655dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // resolve Identifier, then extend the SS with Identifier. This will have
656dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // the effect of resolving Identifier during template instantiation.
657dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // The goal is to be able to resolve a function call whose
658dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // nested-name-specifier is located inside a dependent base class.
659dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // Example:
660dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //
661dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // class C {
662dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // public:
663dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //    static void foo2() {  }
664dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // };
665dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // template <class T> class A { public: typedef C D; };
666dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //
667dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // template <class T> class B : public A<T> {
668dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // public:
669dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  //   void foo() { D::foo2(); }
670dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  // };
6714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().MicrosoftExt) {
672dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
673dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
674dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet      SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
675dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet      return false;
676dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet    }
677dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet  }
678dfb6ae1d8d114772bd91b7079c7e4bf4b517e63cFrancois Pichet
6793d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  unsigned DiagID;
6801bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall  if (!Found.empty())
6813d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    DiagID = diag::err_expected_class_or_namespace;
682a31d5f70e02d89631d07be162796a2d6bd74e561Anders Carlsson  else if (SS.isSet()) {
6832e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    Diag(IdentifierLoc, diag::err_no_member)
6842e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      << &Identifier << LookupCtx << SS.getRange();
6852e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
686a31d5f70e02d89631d07be162796a2d6bd74e561Anders Carlsson  } else
6873d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    DiagID = diag::err_undeclared_var_use;
6881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6893d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  if (SS.isSet())
6902e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange();
6913d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  else
6922e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    Diag(IdentifierLoc, DiagID) << &Identifier;
6931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6942e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return true;
6953d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
6963d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
6972e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
6982e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       IdentifierInfo &Identifier,
6992e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation IdentifierLoc,
7002e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
7012e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       ParsedType ObjectType,
7022e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       bool EnteringContext,
7032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       CXXScopeSpec &SS) {
7042e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
7052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
7062e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
7072e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
7082e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     GetTypeFromParser(ObjectType),
7092e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     EnteringContext, SS,
7102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     /*ScopeLookupResult=*/0, false);
71146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner}
71246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
71342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikiebool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
71442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               const DeclSpec &DS,
71542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie                                               SourceLocation ColonColonLoc) {
71642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
71742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    return true;
71842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
71942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
72042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
72142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
72242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (!T->isDependentType() && !T->getAs<TagType>()) {
72342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class)
7244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      << T << getLangOpts().CPlusPlus;
72542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    return true;
72642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
72742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
72842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  TypeLocBuilder TLB;
72942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
73042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
73142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
73242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie            ColonColonLoc);
73342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  return false;
73442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie}
73542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
73646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// IsInvalidUnlessNestedName - This method is used for error recovery
73746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// purposes to determine whether the specified identifier is only valid as
73846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// a nested name specifier, for example a namespace name.  It is
73946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// conservatively correct to always return false from this method.
74046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner///
74146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
7429ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
7432e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     IdentifierInfo &Identifier,
7442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     SourceLocation IdentifierLoc,
7452e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     SourceLocation ColonLoc,
7462e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                     ParsedType ObjectType,
74746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner                                     bool EnteringContext) {
7482e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
7492e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return false;
7502e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
7512e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
7522e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      GetTypeFromParser(ObjectType),
7532e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      EnteringContext, SS,
7542e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                      /*ScopeLookupResult=*/0, true);
755c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor}
756c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor
7572e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregorbool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
758e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       CXXScopeSpec &SS,
759e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       SourceLocation TemplateKWLoc,
760aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       TemplateTy Template,
761aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation TemplateNameLoc,
762aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation LAngleLoc,
763aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       ASTTemplateArgsPtr TemplateArgsIn,
764aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       SourceLocation RAngleLoc,
7652e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                       SourceLocation CCLoc,
766aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                       bool EnteringContext) {
7672e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (SS.isInvalid())
7682e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
7692e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
770aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // Translate the parser's template argument list in our AST format.
771aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
772aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  translateTemplateArguments(TemplateArgsIn, TemplateArgs);
773aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
774aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){
775aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // Handle a dependent template specialization for which we cannot resolve
776aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // the template name.
777aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    assert(DTN->getQualifier()
778aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor             == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
779aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
78094fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor                                                          DTN->getQualifier(),
78194fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor                                                          DTN->getIdentifier(),
782aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                                TemplateArgs);
783aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
784aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    // Create source-location information for this type.
785aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    TypeLocBuilder Builder;
78655d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    DependentTemplateSpecializationTypeLoc SpecTL
787aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
78855d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setElaboratedKeywordLoc(SourceLocation());
78955d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
79066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara    SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
79155d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    SpecTL.setTemplateNameLoc(TemplateNameLoc);
792aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setLAngleLoc(LAngleLoc);
793aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setRAngleLoc(RAngleLoc);
794aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
795aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
796aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
797e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
798aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor              CCLoc);
799aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    return false;
800aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  }
801aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
8026cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
8036cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor  if (Template.get().getAsOverloadedTemplate() ||
8046cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) {
8056cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    SourceRange R(TemplateNameLoc, RAngleLoc);
8066cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    if (SS.getRange().isValid())
8076cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      R.setBegin(SS.getRange().getBegin());
8086cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
8096cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
8106cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      << Template.get() << R;
8116cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    NoteAllFoundTemplates(Template.get());
8126cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor    return true;
8136cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor  }
8146cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
815aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // We were able to resolve the template name to an actual template.
816aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  // Build an appropriate nested-name-specifier.
817aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
818aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                   TemplateArgs);
8192e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  if (T.isNull())
8202e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    return true;
8212e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
8223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Alias template specializations can produce types which are not valid
8233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // nested name specifiers.
8243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!T->isDependentType() && !T->getAs<TagType>()) {
8253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
8263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    NoteAllFoundTemplates(Template.get());
8273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return true;
8283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
829aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
83055d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  // Provide source-location information for the template specialization type.
831aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  TypeLocBuilder Builder;
83255d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TemplateSpecializationTypeLoc SpecTL
833aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    = Builder.push<TemplateSpecializationTypeLoc>(T);
83455d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
83555d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  SpecTL.setTemplateNameLoc(TemplateNameLoc);
836aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  SpecTL.setLAngleLoc(LAngleLoc);
837aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  SpecTL.setRAngleLoc(RAngleLoc);
838aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
839aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor    SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
840aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
841aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
842e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
843aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor            CCLoc);
8442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor  return false;
84539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor}
84639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
847c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregornamespace {
848c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  /// \brief A structure that stores a nested-name-specifier annotation,
849c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  /// including both the nested-name-specifier
850c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  struct NestedNameSpecifierAnnotation {
851c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    NestedNameSpecifier *NNS;
852c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  };
853c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
854c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
855c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregorvoid *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
856c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  if (SS.isEmpty() || SS.isInvalid())
857c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    return 0;
858c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
859c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
860c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                        SS.location_size()),
861c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                               llvm::alignOf<NestedNameSpecifierAnnotation>());
862c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  NestedNameSpecifierAnnotation *Annotation
863c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    = new (Mem) NestedNameSpecifierAnnotation;
864c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Annotation->NNS = SS.getScopeRep();
865c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  memcpy(Annotation + 1, SS.location_data(), SS.location_size());
866c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  return Annotation;
867c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
868c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
869c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregorvoid Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
870c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                SourceRange AnnotationRange,
871c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                CXXScopeSpec &SS) {
872c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  if (!AnnotationPtr) {
873c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    SS.SetInvalid(AnnotationRange);
874c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    return;
875c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  }
876c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
877c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  NestedNameSpecifierAnnotation *Annotation
878c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
879c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
880c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor}
881c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
882e7e278bce2301990107cef3f873cbbf7da94469aJohn McCallbool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
883e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
884e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
885e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  NestedNameSpecifier *Qualifier =
886e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    static_cast<NestedNameSpecifier*>(SS.getScopeRep());
887e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
888e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // There are only two places a well-formed program may qualify a
889e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // declarator: first, when defining a namespace or class member
890e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // out-of-line, and second, when naming an explicitly-qualified
891e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // friend function.  The latter case is governed by
892e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // C++03 [basic.lookup.unqual]p10:
893e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   In a friend declaration naming a member function, a name used
894e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   in the function declarator and not part of a template-argument
895e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   in a template-id is first looked up in the scope of the member
896e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   function's class. If it is not found, or if the name is part of
897e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   a template-argument in a template-id, the look up is as
898e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   described for unqualified names in the definition of the class
899e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  //   granting friendship.
900e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  // i.e. we don't push a scope unless it's a class member.
901e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
902e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  switch (Qualifier->getKind()) {
903e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Global:
904e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Namespace:
90514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
906e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // These are always namespace scopes.  We never want to enter a
907e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // namespace scope from anything but a file context.
9087a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return CurContext->getRedeclContext()->isFileContext();
909e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
910e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::Identifier:
911e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::TypeSpec:
912e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  case NestedNameSpecifier::TypeSpecWithTemplate:
913e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    // These are never namespace scopes.
914e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall    return true;
915e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall  }
916e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
9177530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
918e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall}
919e7e278bce2301990107cef3f873cbbf7da94469aJohn McCall
9203d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
9213d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// scope or nested-name-specifier) is parsed, part of a declarator-id.
9223d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// After this method is called, according to [C++ 3.4.3p3], names should be
9233d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// looked up in the declarator-id's scope, until the declarator is parsed and
9243d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXExitDeclaratorScope is called.
9253d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// The 'SS' should be a non-empty valid CXXScopeSpec.
9269ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
9273d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
9287a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9297a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (SS.isInvalid()) return true;
9307a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9317a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  DeclContext *DC = computeDeclContext(SS, true);
9327a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  if (!DC) return true;
9337a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9347a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // Before we enter a declarator's context, we need to make sure that
9357a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  // it is a complete declaration context.
93677bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall  if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
9377a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall    return true;
9387a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall
9397a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  EnterDeclaratorContext(S, DC);
94031f17ecbef57b5679c017c375db330546b7b5145John McCall
94131f17ecbef57b5679c017c375db330546b7b5145John McCall  // Rebuild the nested name specifier for the new scope.
94231f17ecbef57b5679c017c375db330546b7b5145John McCall  if (DC->isDependentContext())
94331f17ecbef57b5679c017c375db330546b7b5145John McCall    RebuildNestedNameSpecifierInCurrentInstantiation(SS);
94431f17ecbef57b5679c017c375db330546b7b5145John McCall
9457dfd0fb08300b60a9657748bda7d8b3ceb07babeDouglas Gregor  return false;
9463d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
9473d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
9483d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
9493d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
9503d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
9513d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// Used to indicate that names should revert to being looked up in the
9523d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/// defining scope.
9533d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetvoid Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
9543d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet  assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
955dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor  if (SS.isInvalid())
956dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    return;
9577a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
9587a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall         "exiting declarator scope we never really entered");
9597a1dc562d4ad59237ed9fe7e8cef56f9eaa7a26cJohn McCall  ExitDeclaratorContext(S);
9603d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet}
961