1*f4a2713aSLionel Sambuc //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements C++ semantic analysis for scope specifiers. 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h" 15*f4a2713aSLionel Sambuc #include "TypeLocBuilder.h" 16*f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h" 17*f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h" 18*f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h" 19*f4a2713aSLionel Sambuc #include "clang/AST/NestedNameSpecifier.h" 20*f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h" 21*f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h" 22*f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h" 23*f4a2713aSLionel Sambuc #include "clang/Sema/Template.h" 24*f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h" 25*f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h" 26*f4a2713aSLionel Sambuc using namespace clang; 27*f4a2713aSLionel Sambuc 28*f4a2713aSLionel Sambuc /// \brief Find the current instantiation that associated with the given type. 29*f4a2713aSLionel Sambuc static CXXRecordDecl *getCurrentInstantiationOf(QualType T, 30*f4a2713aSLionel Sambuc DeclContext *CurContext) { 31*f4a2713aSLionel Sambuc if (T.isNull()) 32*f4a2713aSLionel Sambuc return 0; 33*f4a2713aSLionel Sambuc 34*f4a2713aSLionel Sambuc const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 35*f4a2713aSLionel Sambuc if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 36*f4a2713aSLionel Sambuc CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 37*f4a2713aSLionel Sambuc if (!Record->isDependentContext() || 38*f4a2713aSLionel Sambuc Record->isCurrentInstantiation(CurContext)) 39*f4a2713aSLionel Sambuc return Record; 40*f4a2713aSLionel Sambuc 41*f4a2713aSLionel Sambuc return 0; 42*f4a2713aSLionel Sambuc } else if (isa<InjectedClassNameType>(Ty)) 43*f4a2713aSLionel Sambuc return cast<InjectedClassNameType>(Ty)->getDecl(); 44*f4a2713aSLionel Sambuc else 45*f4a2713aSLionel Sambuc return 0; 46*f4a2713aSLionel Sambuc } 47*f4a2713aSLionel Sambuc 48*f4a2713aSLionel Sambuc /// \brief Compute the DeclContext that is associated with the given type. 49*f4a2713aSLionel Sambuc /// 50*f4a2713aSLionel Sambuc /// \param T the type for which we are attempting to find a DeclContext. 51*f4a2713aSLionel Sambuc /// 52*f4a2713aSLionel Sambuc /// \returns the declaration context represented by the type T, 53*f4a2713aSLionel Sambuc /// or NULL if the declaration context cannot be computed (e.g., because it is 54*f4a2713aSLionel Sambuc /// dependent and not the current instantiation). 55*f4a2713aSLionel Sambuc DeclContext *Sema::computeDeclContext(QualType T) { 56*f4a2713aSLionel Sambuc if (!T->isDependentType()) 57*f4a2713aSLionel Sambuc if (const TagType *Tag = T->getAs<TagType>()) 58*f4a2713aSLionel Sambuc return Tag->getDecl(); 59*f4a2713aSLionel Sambuc 60*f4a2713aSLionel Sambuc return ::getCurrentInstantiationOf(T, CurContext); 61*f4a2713aSLionel Sambuc } 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc /// \brief Compute the DeclContext that is associated with the given 64*f4a2713aSLionel Sambuc /// scope specifier. 65*f4a2713aSLionel Sambuc /// 66*f4a2713aSLionel Sambuc /// \param SS the C++ scope specifier as it appears in the source 67*f4a2713aSLionel Sambuc /// 68*f4a2713aSLionel Sambuc /// \param EnteringContext when true, we will be entering the context of 69*f4a2713aSLionel Sambuc /// this scope specifier, so we can retrieve the declaration context of a 70*f4a2713aSLionel Sambuc /// class template or class template partial specialization even if it is 71*f4a2713aSLionel Sambuc /// not the current instantiation. 72*f4a2713aSLionel Sambuc /// 73*f4a2713aSLionel Sambuc /// \returns the declaration context represented by the scope specifier @p SS, 74*f4a2713aSLionel Sambuc /// or NULL if the declaration context cannot be computed (e.g., because it is 75*f4a2713aSLionel Sambuc /// dependent and not the current instantiation). 76*f4a2713aSLionel Sambuc DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 77*f4a2713aSLionel Sambuc bool EnteringContext) { 78*f4a2713aSLionel Sambuc if (!SS.isSet() || SS.isInvalid()) 79*f4a2713aSLionel Sambuc return 0; 80*f4a2713aSLionel Sambuc 81*f4a2713aSLionel Sambuc NestedNameSpecifier *NNS = SS.getScopeRep(); 82*f4a2713aSLionel Sambuc if (NNS->isDependent()) { 83*f4a2713aSLionel Sambuc // If this nested-name-specifier refers to the current 84*f4a2713aSLionel Sambuc // instantiation, return its DeclContext. 85*f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 86*f4a2713aSLionel Sambuc return Record; 87*f4a2713aSLionel Sambuc 88*f4a2713aSLionel Sambuc if (EnteringContext) { 89*f4a2713aSLionel Sambuc const Type *NNSType = NNS->getAsType(); 90*f4a2713aSLionel Sambuc if (!NNSType) { 91*f4a2713aSLionel Sambuc return 0; 92*f4a2713aSLionel Sambuc } 93*f4a2713aSLionel Sambuc 94*f4a2713aSLionel Sambuc // Look through type alias templates, per C++0x [temp.dep.type]p1. 95*f4a2713aSLionel Sambuc NNSType = Context.getCanonicalType(NNSType); 96*f4a2713aSLionel Sambuc if (const TemplateSpecializationType *SpecType 97*f4a2713aSLionel Sambuc = NNSType->getAs<TemplateSpecializationType>()) { 98*f4a2713aSLionel Sambuc // We are entering the context of the nested name specifier, so try to 99*f4a2713aSLionel Sambuc // match the nested name specifier to either a primary class template 100*f4a2713aSLionel Sambuc // or a class template partial specialization. 101*f4a2713aSLionel Sambuc if (ClassTemplateDecl *ClassTemplate 102*f4a2713aSLionel Sambuc = dyn_cast_or_null<ClassTemplateDecl>( 103*f4a2713aSLionel Sambuc SpecType->getTemplateName().getAsTemplateDecl())) { 104*f4a2713aSLionel Sambuc QualType ContextType 105*f4a2713aSLionel Sambuc = Context.getCanonicalType(QualType(SpecType, 0)); 106*f4a2713aSLionel Sambuc 107*f4a2713aSLionel Sambuc // If the type of the nested name specifier is the same as the 108*f4a2713aSLionel Sambuc // injected class name of the named class template, we're entering 109*f4a2713aSLionel Sambuc // into that class template definition. 110*f4a2713aSLionel Sambuc QualType Injected 111*f4a2713aSLionel Sambuc = ClassTemplate->getInjectedClassNameSpecialization(); 112*f4a2713aSLionel Sambuc if (Context.hasSameType(Injected, ContextType)) 113*f4a2713aSLionel Sambuc return ClassTemplate->getTemplatedDecl(); 114*f4a2713aSLionel Sambuc 115*f4a2713aSLionel Sambuc // If the type of the nested name specifier is the same as the 116*f4a2713aSLionel Sambuc // type of one of the class template's class template partial 117*f4a2713aSLionel Sambuc // specializations, we're entering into the definition of that 118*f4a2713aSLionel Sambuc // class template partial specialization. 119*f4a2713aSLionel Sambuc if (ClassTemplatePartialSpecializationDecl *PartialSpec 120*f4a2713aSLionel Sambuc = ClassTemplate->findPartialSpecialization(ContextType)) 121*f4a2713aSLionel Sambuc return PartialSpec; 122*f4a2713aSLionel Sambuc } 123*f4a2713aSLionel Sambuc } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 124*f4a2713aSLionel Sambuc // The nested name specifier refers to a member of a class template. 125*f4a2713aSLionel Sambuc return RecordT->getDecl(); 126*f4a2713aSLionel Sambuc } 127*f4a2713aSLionel Sambuc } 128*f4a2713aSLionel Sambuc 129*f4a2713aSLionel Sambuc return 0; 130*f4a2713aSLionel Sambuc } 131*f4a2713aSLionel Sambuc 132*f4a2713aSLionel Sambuc switch (NNS->getKind()) { 133*f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier: 134*f4a2713aSLionel Sambuc llvm_unreachable("Dependent nested-name-specifier has no DeclContext"); 135*f4a2713aSLionel Sambuc 136*f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace: 137*f4a2713aSLionel Sambuc return NNS->getAsNamespace(); 138*f4a2713aSLionel Sambuc 139*f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias: 140*f4a2713aSLionel Sambuc return NNS->getAsNamespaceAlias()->getNamespace(); 141*f4a2713aSLionel Sambuc 142*f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec: 143*f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate: { 144*f4a2713aSLionel Sambuc const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 145*f4a2713aSLionel Sambuc assert(Tag && "Non-tag type in nested-name-specifier"); 146*f4a2713aSLionel Sambuc return Tag->getDecl(); 147*f4a2713aSLionel Sambuc } 148*f4a2713aSLionel Sambuc 149*f4a2713aSLionel Sambuc case NestedNameSpecifier::Global: 150*f4a2713aSLionel Sambuc return Context.getTranslationUnitDecl(); 151*f4a2713aSLionel Sambuc } 152*f4a2713aSLionel Sambuc 153*f4a2713aSLionel Sambuc llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 154*f4a2713aSLionel Sambuc } 155*f4a2713aSLionel Sambuc 156*f4a2713aSLionel Sambuc bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 157*f4a2713aSLionel Sambuc if (!SS.isSet() || SS.isInvalid()) 158*f4a2713aSLionel Sambuc return false; 159*f4a2713aSLionel Sambuc 160*f4a2713aSLionel Sambuc return SS.getScopeRep()->isDependent(); 161*f4a2713aSLionel Sambuc } 162*f4a2713aSLionel Sambuc 163*f4a2713aSLionel Sambuc /// \brief If the given nested name specifier refers to the current 164*f4a2713aSLionel Sambuc /// instantiation, return the declaration that corresponds to that 165*f4a2713aSLionel Sambuc /// current instantiation (C++0x [temp.dep.type]p1). 166*f4a2713aSLionel Sambuc /// 167*f4a2713aSLionel Sambuc /// \param NNS a dependent nested name specifier. 168*f4a2713aSLionel Sambuc CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 169*f4a2713aSLionel Sambuc assert(getLangOpts().CPlusPlus && "Only callable in C++"); 170*f4a2713aSLionel Sambuc assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 171*f4a2713aSLionel Sambuc 172*f4a2713aSLionel Sambuc if (!NNS->getAsType()) 173*f4a2713aSLionel Sambuc return 0; 174*f4a2713aSLionel Sambuc 175*f4a2713aSLionel Sambuc QualType T = QualType(NNS->getAsType(), 0); 176*f4a2713aSLionel Sambuc return ::getCurrentInstantiationOf(T, CurContext); 177*f4a2713aSLionel Sambuc } 178*f4a2713aSLionel Sambuc 179*f4a2713aSLionel Sambuc /// \brief Require that the context specified by SS be complete. 180*f4a2713aSLionel Sambuc /// 181*f4a2713aSLionel Sambuc /// If SS refers to a type, this routine checks whether the type is 182*f4a2713aSLionel Sambuc /// complete enough (or can be made complete enough) for name lookup 183*f4a2713aSLionel Sambuc /// into the DeclContext. A type that is not yet completed can be 184*f4a2713aSLionel Sambuc /// considered "complete enough" if it is a class/struct/union/enum 185*f4a2713aSLionel Sambuc /// that is currently being defined. Or, if we have a type that names 186*f4a2713aSLionel Sambuc /// a class template specialization that is not a complete type, we 187*f4a2713aSLionel Sambuc /// will attempt to instantiate that class template. 188*f4a2713aSLionel Sambuc bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 189*f4a2713aSLionel Sambuc DeclContext *DC) { 190*f4a2713aSLionel Sambuc assert(DC != 0 && "given null context"); 191*f4a2713aSLionel Sambuc 192*f4a2713aSLionel Sambuc TagDecl *tag = dyn_cast<TagDecl>(DC); 193*f4a2713aSLionel Sambuc 194*f4a2713aSLionel Sambuc // If this is a dependent type, then we consider it complete. 195*f4a2713aSLionel Sambuc if (!tag || tag->isDependentContext()) 196*f4a2713aSLionel Sambuc return false; 197*f4a2713aSLionel Sambuc 198*f4a2713aSLionel Sambuc // If we're currently defining this type, then lookup into the 199*f4a2713aSLionel Sambuc // type is okay: don't complain that it isn't complete yet. 200*f4a2713aSLionel Sambuc QualType type = Context.getTypeDeclType(tag); 201*f4a2713aSLionel Sambuc const TagType *tagType = type->getAs<TagType>(); 202*f4a2713aSLionel Sambuc if (tagType && tagType->isBeingDefined()) 203*f4a2713aSLionel Sambuc return false; 204*f4a2713aSLionel Sambuc 205*f4a2713aSLionel Sambuc SourceLocation loc = SS.getLastQualifierNameLoc(); 206*f4a2713aSLionel Sambuc if (loc.isInvalid()) loc = SS.getRange().getBegin(); 207*f4a2713aSLionel Sambuc 208*f4a2713aSLionel Sambuc // The type must be complete. 209*f4a2713aSLionel Sambuc if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec, 210*f4a2713aSLionel Sambuc SS.getRange())) { 211*f4a2713aSLionel Sambuc SS.SetInvalid(SS.getRange()); 212*f4a2713aSLionel Sambuc return true; 213*f4a2713aSLionel Sambuc } 214*f4a2713aSLionel Sambuc 215*f4a2713aSLionel Sambuc // Fixed enum types are complete, but they aren't valid as scopes 216*f4a2713aSLionel Sambuc // until we see a definition, so awkwardly pull out this special 217*f4a2713aSLionel Sambuc // case. 218*f4a2713aSLionel Sambuc const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType); 219*f4a2713aSLionel Sambuc if (!enumType || enumType->getDecl()->isCompleteDefinition()) 220*f4a2713aSLionel Sambuc return false; 221*f4a2713aSLionel Sambuc 222*f4a2713aSLionel Sambuc // Try to instantiate the definition, if this is a specialization of an 223*f4a2713aSLionel Sambuc // enumeration temploid. 224*f4a2713aSLionel Sambuc EnumDecl *ED = enumType->getDecl(); 225*f4a2713aSLionel Sambuc if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) { 226*f4a2713aSLionel Sambuc MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo(); 227*f4a2713aSLionel Sambuc if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) { 228*f4a2713aSLionel Sambuc if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED), 229*f4a2713aSLionel Sambuc TSK_ImplicitInstantiation)) { 230*f4a2713aSLionel Sambuc SS.SetInvalid(SS.getRange()); 231*f4a2713aSLionel Sambuc return true; 232*f4a2713aSLionel Sambuc } 233*f4a2713aSLionel Sambuc return false; 234*f4a2713aSLionel Sambuc } 235*f4a2713aSLionel Sambuc } 236*f4a2713aSLionel Sambuc 237*f4a2713aSLionel Sambuc Diag(loc, diag::err_incomplete_nested_name_spec) 238*f4a2713aSLionel Sambuc << type << SS.getRange(); 239*f4a2713aSLionel Sambuc SS.SetInvalid(SS.getRange()); 240*f4a2713aSLionel Sambuc return true; 241*f4a2713aSLionel Sambuc } 242*f4a2713aSLionel Sambuc 243*f4a2713aSLionel Sambuc bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc, 244*f4a2713aSLionel Sambuc CXXScopeSpec &SS) { 245*f4a2713aSLionel Sambuc SS.MakeGlobal(Context, CCLoc); 246*f4a2713aSLionel Sambuc return false; 247*f4a2713aSLionel Sambuc } 248*f4a2713aSLionel Sambuc 249*f4a2713aSLionel Sambuc /// \brief Determines whether the given declaration is an valid acceptable 250*f4a2713aSLionel Sambuc /// result for name lookup of a nested-name-specifier. 251*f4a2713aSLionel Sambuc bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD) { 252*f4a2713aSLionel Sambuc if (!SD) 253*f4a2713aSLionel Sambuc return false; 254*f4a2713aSLionel Sambuc 255*f4a2713aSLionel Sambuc // Namespace and namespace aliases are fine. 256*f4a2713aSLionel Sambuc if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) 257*f4a2713aSLionel Sambuc return true; 258*f4a2713aSLionel Sambuc 259*f4a2713aSLionel Sambuc if (!isa<TypeDecl>(SD)) 260*f4a2713aSLionel Sambuc return false; 261*f4a2713aSLionel Sambuc 262*f4a2713aSLionel Sambuc // Determine whether we have a class (or, in C++11, an enum) or 263*f4a2713aSLionel Sambuc // a typedef thereof. If so, build the nested-name-specifier. 264*f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 265*f4a2713aSLionel Sambuc if (T->isDependentType()) 266*f4a2713aSLionel Sambuc return true; 267*f4a2713aSLionel Sambuc else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { 268*f4a2713aSLionel Sambuc if (TD->getUnderlyingType()->isRecordType() || 269*f4a2713aSLionel Sambuc (Context.getLangOpts().CPlusPlus11 && 270*f4a2713aSLionel Sambuc TD->getUnderlyingType()->isEnumeralType())) 271*f4a2713aSLionel Sambuc return true; 272*f4a2713aSLionel Sambuc } else if (isa<RecordDecl>(SD) || 273*f4a2713aSLionel Sambuc (Context.getLangOpts().CPlusPlus11 && isa<EnumDecl>(SD))) 274*f4a2713aSLionel Sambuc return true; 275*f4a2713aSLionel Sambuc 276*f4a2713aSLionel Sambuc return false; 277*f4a2713aSLionel Sambuc } 278*f4a2713aSLionel Sambuc 279*f4a2713aSLionel Sambuc /// \brief If the given nested-name-specifier begins with a bare identifier 280*f4a2713aSLionel Sambuc /// (e.g., Base::), perform name lookup for that identifier as a 281*f4a2713aSLionel Sambuc /// nested-name-specifier within the given scope, and return the result of that 282*f4a2713aSLionel Sambuc /// name lookup. 283*f4a2713aSLionel Sambuc NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 284*f4a2713aSLionel Sambuc if (!S || !NNS) 285*f4a2713aSLionel Sambuc return 0; 286*f4a2713aSLionel Sambuc 287*f4a2713aSLionel Sambuc while (NNS->getPrefix()) 288*f4a2713aSLionel Sambuc NNS = NNS->getPrefix(); 289*f4a2713aSLionel Sambuc 290*f4a2713aSLionel Sambuc if (NNS->getKind() != NestedNameSpecifier::Identifier) 291*f4a2713aSLionel Sambuc return 0; 292*f4a2713aSLionel Sambuc 293*f4a2713aSLionel Sambuc LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 294*f4a2713aSLionel Sambuc LookupNestedNameSpecifierName); 295*f4a2713aSLionel Sambuc LookupName(Found, S); 296*f4a2713aSLionel Sambuc assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 297*f4a2713aSLionel Sambuc 298*f4a2713aSLionel Sambuc if (!Found.isSingleResult()) 299*f4a2713aSLionel Sambuc return 0; 300*f4a2713aSLionel Sambuc 301*f4a2713aSLionel Sambuc NamedDecl *Result = Found.getFoundDecl(); 302*f4a2713aSLionel Sambuc if (isAcceptableNestedNameSpecifier(Result)) 303*f4a2713aSLionel Sambuc return Result; 304*f4a2713aSLionel Sambuc 305*f4a2713aSLionel Sambuc return 0; 306*f4a2713aSLionel Sambuc } 307*f4a2713aSLionel Sambuc 308*f4a2713aSLionel Sambuc bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, 309*f4a2713aSLionel Sambuc SourceLocation IdLoc, 310*f4a2713aSLionel Sambuc IdentifierInfo &II, 311*f4a2713aSLionel Sambuc ParsedType ObjectTypePtr) { 312*f4a2713aSLionel Sambuc QualType ObjectType = GetTypeFromParser(ObjectTypePtr); 313*f4a2713aSLionel Sambuc LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 314*f4a2713aSLionel Sambuc 315*f4a2713aSLionel Sambuc // Determine where to perform name lookup 316*f4a2713aSLionel Sambuc DeclContext *LookupCtx = 0; 317*f4a2713aSLionel Sambuc bool isDependent = false; 318*f4a2713aSLionel Sambuc if (!ObjectType.isNull()) { 319*f4a2713aSLionel Sambuc // This nested-name-specifier occurs in a member access expression, e.g., 320*f4a2713aSLionel Sambuc // x->B::f, and we are looking into the type of the object. 321*f4a2713aSLionel Sambuc assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 322*f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(ObjectType); 323*f4a2713aSLionel Sambuc isDependent = ObjectType->isDependentType(); 324*f4a2713aSLionel Sambuc } else if (SS.isSet()) { 325*f4a2713aSLionel Sambuc // This nested-name-specifier occurs after another nested-name-specifier, 326*f4a2713aSLionel Sambuc // so long into the context associated with the prior nested-name-specifier. 327*f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, false); 328*f4a2713aSLionel Sambuc isDependent = isDependentScopeSpecifier(SS); 329*f4a2713aSLionel Sambuc Found.setContextRange(SS.getRange()); 330*f4a2713aSLionel Sambuc } 331*f4a2713aSLionel Sambuc 332*f4a2713aSLionel Sambuc if (LookupCtx) { 333*f4a2713aSLionel Sambuc // Perform "qualified" name lookup into the declaration context we 334*f4a2713aSLionel Sambuc // computed, which is either the type of the base of a member access 335*f4a2713aSLionel Sambuc // expression or the declaration context associated with a prior 336*f4a2713aSLionel Sambuc // nested-name-specifier. 337*f4a2713aSLionel Sambuc 338*f4a2713aSLionel Sambuc // The declaration context must be complete. 339*f4a2713aSLionel Sambuc if (!LookupCtx->isDependentContext() && 340*f4a2713aSLionel Sambuc RequireCompleteDeclContext(SS, LookupCtx)) 341*f4a2713aSLionel Sambuc return false; 342*f4a2713aSLionel Sambuc 343*f4a2713aSLionel Sambuc LookupQualifiedName(Found, LookupCtx); 344*f4a2713aSLionel Sambuc } else if (isDependent) { 345*f4a2713aSLionel Sambuc return false; 346*f4a2713aSLionel Sambuc } else { 347*f4a2713aSLionel Sambuc LookupName(Found, S); 348*f4a2713aSLionel Sambuc } 349*f4a2713aSLionel Sambuc Found.suppressDiagnostics(); 350*f4a2713aSLionel Sambuc 351*f4a2713aSLionel Sambuc if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 352*f4a2713aSLionel Sambuc return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); 353*f4a2713aSLionel Sambuc 354*f4a2713aSLionel Sambuc return false; 355*f4a2713aSLionel Sambuc } 356*f4a2713aSLionel Sambuc 357*f4a2713aSLionel Sambuc namespace { 358*f4a2713aSLionel Sambuc 359*f4a2713aSLionel Sambuc // Callback to only accept typo corrections that can be a valid C++ member 360*f4a2713aSLionel Sambuc // intializer: either a non-static field member or a base class. 361*f4a2713aSLionel Sambuc class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback { 362*f4a2713aSLionel Sambuc public: 363*f4a2713aSLionel Sambuc explicit NestedNameSpecifierValidatorCCC(Sema &SRef) 364*f4a2713aSLionel Sambuc : SRef(SRef) {} 365*f4a2713aSLionel Sambuc 366*f4a2713aSLionel Sambuc virtual bool ValidateCandidate(const TypoCorrection &candidate) { 367*f4a2713aSLionel Sambuc return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl()); 368*f4a2713aSLionel Sambuc } 369*f4a2713aSLionel Sambuc 370*f4a2713aSLionel Sambuc private: 371*f4a2713aSLionel Sambuc Sema &SRef; 372*f4a2713aSLionel Sambuc }; 373*f4a2713aSLionel Sambuc 374*f4a2713aSLionel Sambuc } 375*f4a2713aSLionel Sambuc 376*f4a2713aSLionel Sambuc /// \brief Build a new nested-name-specifier for "identifier::", as described 377*f4a2713aSLionel Sambuc /// by ActOnCXXNestedNameSpecifier. 378*f4a2713aSLionel Sambuc /// 379*f4a2713aSLionel Sambuc /// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in 380*f4a2713aSLionel Sambuc /// that it contains an extra parameter \p ScopeLookupResult, which provides 381*f4a2713aSLionel Sambuc /// the result of name lookup within the scope of the nested-name-specifier 382*f4a2713aSLionel Sambuc /// that was computed at template definition time. 383*f4a2713aSLionel Sambuc /// 384*f4a2713aSLionel Sambuc /// If ErrorRecoveryLookup is true, then this call is used to improve error 385*f4a2713aSLionel Sambuc /// recovery. This means that it should not emit diagnostics, it should 386*f4a2713aSLionel Sambuc /// just return true on failure. It also means it should only return a valid 387*f4a2713aSLionel Sambuc /// scope if it *knows* that the result is correct. It should not return in a 388*f4a2713aSLionel Sambuc /// dependent context, for example. Nor will it extend \p SS with the scope 389*f4a2713aSLionel Sambuc /// specifier. 390*f4a2713aSLionel Sambuc bool Sema::BuildCXXNestedNameSpecifier(Scope *S, 391*f4a2713aSLionel Sambuc IdentifierInfo &Identifier, 392*f4a2713aSLionel Sambuc SourceLocation IdentifierLoc, 393*f4a2713aSLionel Sambuc SourceLocation CCLoc, 394*f4a2713aSLionel Sambuc QualType ObjectType, 395*f4a2713aSLionel Sambuc bool EnteringContext, 396*f4a2713aSLionel Sambuc CXXScopeSpec &SS, 397*f4a2713aSLionel Sambuc NamedDecl *ScopeLookupResult, 398*f4a2713aSLionel Sambuc bool ErrorRecoveryLookup) { 399*f4a2713aSLionel Sambuc LookupResult Found(*this, &Identifier, IdentifierLoc, 400*f4a2713aSLionel Sambuc LookupNestedNameSpecifierName); 401*f4a2713aSLionel Sambuc 402*f4a2713aSLionel Sambuc // Determine where to perform name lookup 403*f4a2713aSLionel Sambuc DeclContext *LookupCtx = 0; 404*f4a2713aSLionel Sambuc bool isDependent = false; 405*f4a2713aSLionel Sambuc if (!ObjectType.isNull()) { 406*f4a2713aSLionel Sambuc // This nested-name-specifier occurs in a member access expression, e.g., 407*f4a2713aSLionel Sambuc // x->B::f, and we are looking into the type of the object. 408*f4a2713aSLionel Sambuc assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 409*f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(ObjectType); 410*f4a2713aSLionel Sambuc isDependent = ObjectType->isDependentType(); 411*f4a2713aSLionel Sambuc } else if (SS.isSet()) { 412*f4a2713aSLionel Sambuc // This nested-name-specifier occurs after another nested-name-specifier, 413*f4a2713aSLionel Sambuc // so look into the context associated with the prior nested-name-specifier. 414*f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, EnteringContext); 415*f4a2713aSLionel Sambuc isDependent = isDependentScopeSpecifier(SS); 416*f4a2713aSLionel Sambuc Found.setContextRange(SS.getRange()); 417*f4a2713aSLionel Sambuc } 418*f4a2713aSLionel Sambuc 419*f4a2713aSLionel Sambuc 420*f4a2713aSLionel Sambuc bool ObjectTypeSearchedInScope = false; 421*f4a2713aSLionel Sambuc if (LookupCtx) { 422*f4a2713aSLionel Sambuc // Perform "qualified" name lookup into the declaration context we 423*f4a2713aSLionel Sambuc // computed, which is either the type of the base of a member access 424*f4a2713aSLionel Sambuc // expression or the declaration context associated with a prior 425*f4a2713aSLionel Sambuc // nested-name-specifier. 426*f4a2713aSLionel Sambuc 427*f4a2713aSLionel Sambuc // The declaration context must be complete. 428*f4a2713aSLionel Sambuc if (!LookupCtx->isDependentContext() && 429*f4a2713aSLionel Sambuc RequireCompleteDeclContext(SS, LookupCtx)) 430*f4a2713aSLionel Sambuc return true; 431*f4a2713aSLionel Sambuc 432*f4a2713aSLionel Sambuc LookupQualifiedName(Found, LookupCtx); 433*f4a2713aSLionel Sambuc 434*f4a2713aSLionel Sambuc if (!ObjectType.isNull() && Found.empty()) { 435*f4a2713aSLionel Sambuc // C++ [basic.lookup.classref]p4: 436*f4a2713aSLionel Sambuc // If the id-expression in a class member access is a qualified-id of 437*f4a2713aSLionel Sambuc // the form 438*f4a2713aSLionel Sambuc // 439*f4a2713aSLionel Sambuc // class-name-or-namespace-name::... 440*f4a2713aSLionel Sambuc // 441*f4a2713aSLionel Sambuc // the class-name-or-namespace-name following the . or -> operator is 442*f4a2713aSLionel Sambuc // looked up both in the context of the entire postfix-expression and in 443*f4a2713aSLionel Sambuc // the scope of the class of the object expression. If the name is found 444*f4a2713aSLionel Sambuc // only in the scope of the class of the object expression, the name 445*f4a2713aSLionel Sambuc // shall refer to a class-name. If the name is found only in the 446*f4a2713aSLionel Sambuc // context of the entire postfix-expression, the name shall refer to a 447*f4a2713aSLionel Sambuc // class-name or namespace-name. [...] 448*f4a2713aSLionel Sambuc // 449*f4a2713aSLionel Sambuc // Qualified name lookup into a class will not find a namespace-name, 450*f4a2713aSLionel Sambuc // so we do not need to diagnose that case specifically. However, 451*f4a2713aSLionel Sambuc // this qualified name lookup may find nothing. In that case, perform 452*f4a2713aSLionel Sambuc // unqualified name lookup in the given scope (if available) or 453*f4a2713aSLionel Sambuc // reconstruct the result from when name lookup was performed at template 454*f4a2713aSLionel Sambuc // definition time. 455*f4a2713aSLionel Sambuc if (S) 456*f4a2713aSLionel Sambuc LookupName(Found, S); 457*f4a2713aSLionel Sambuc else if (ScopeLookupResult) 458*f4a2713aSLionel Sambuc Found.addDecl(ScopeLookupResult); 459*f4a2713aSLionel Sambuc 460*f4a2713aSLionel Sambuc ObjectTypeSearchedInScope = true; 461*f4a2713aSLionel Sambuc } 462*f4a2713aSLionel Sambuc } else if (!isDependent) { 463*f4a2713aSLionel Sambuc // Perform unqualified name lookup in the current scope. 464*f4a2713aSLionel Sambuc LookupName(Found, S); 465*f4a2713aSLionel Sambuc } 466*f4a2713aSLionel Sambuc 467*f4a2713aSLionel Sambuc // If we performed lookup into a dependent context and did not find anything, 468*f4a2713aSLionel Sambuc // that's fine: just build a dependent nested-name-specifier. 469*f4a2713aSLionel Sambuc if (Found.empty() && isDependent && 470*f4a2713aSLionel Sambuc !(LookupCtx && LookupCtx->isRecord() && 471*f4a2713aSLionel Sambuc (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || 472*f4a2713aSLionel Sambuc !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) { 473*f4a2713aSLionel Sambuc // Don't speculate if we're just trying to improve error recovery. 474*f4a2713aSLionel Sambuc if (ErrorRecoveryLookup) 475*f4a2713aSLionel Sambuc return true; 476*f4a2713aSLionel Sambuc 477*f4a2713aSLionel Sambuc // We were not able to compute the declaration context for a dependent 478*f4a2713aSLionel Sambuc // base object type or prior nested-name-specifier, so this 479*f4a2713aSLionel Sambuc // nested-name-specifier refers to an unknown specialization. Just build 480*f4a2713aSLionel Sambuc // a dependent nested-name-specifier. 481*f4a2713aSLionel Sambuc SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); 482*f4a2713aSLionel Sambuc return false; 483*f4a2713aSLionel Sambuc } 484*f4a2713aSLionel Sambuc 485*f4a2713aSLionel Sambuc // FIXME: Deal with ambiguities cleanly. 486*f4a2713aSLionel Sambuc 487*f4a2713aSLionel Sambuc if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MicrosoftMode) { 488*f4a2713aSLionel Sambuc // We haven't found anything, and we're not recovering from a 489*f4a2713aSLionel Sambuc // different kind of error, so look for typos. 490*f4a2713aSLionel Sambuc DeclarationName Name = Found.getLookupName(); 491*f4a2713aSLionel Sambuc NestedNameSpecifierValidatorCCC Validator(*this); 492*f4a2713aSLionel Sambuc Found.clear(); 493*f4a2713aSLionel Sambuc if (TypoCorrection Corrected = 494*f4a2713aSLionel Sambuc CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S, 495*f4a2713aSLionel Sambuc &SS, Validator, LookupCtx, EnteringContext)) { 496*f4a2713aSLionel Sambuc if (LookupCtx) { 497*f4a2713aSLionel Sambuc bool DroppedSpecifier = 498*f4a2713aSLionel Sambuc Corrected.WillReplaceSpecifier() && 499*f4a2713aSLionel Sambuc Name.getAsString() == Corrected.getAsString(getLangOpts()); 500*f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) 501*f4a2713aSLionel Sambuc << Name << LookupCtx << DroppedSpecifier 502*f4a2713aSLionel Sambuc << SS.getRange()); 503*f4a2713aSLionel Sambuc } else 504*f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 505*f4a2713aSLionel Sambuc << Name); 506*f4a2713aSLionel Sambuc 507*f4a2713aSLionel Sambuc if (NamedDecl *ND = Corrected.getCorrectionDecl()) 508*f4a2713aSLionel Sambuc Found.addDecl(ND); 509*f4a2713aSLionel Sambuc Found.setLookupName(Corrected.getCorrection()); 510*f4a2713aSLionel Sambuc } else { 511*f4a2713aSLionel Sambuc Found.setLookupName(&Identifier); 512*f4a2713aSLionel Sambuc } 513*f4a2713aSLionel Sambuc } 514*f4a2713aSLionel Sambuc 515*f4a2713aSLionel Sambuc NamedDecl *SD = Found.getAsSingle<NamedDecl>(); 516*f4a2713aSLionel Sambuc if (isAcceptableNestedNameSpecifier(SD)) { 517*f4a2713aSLionel Sambuc if (!ObjectType.isNull() && !ObjectTypeSearchedInScope && 518*f4a2713aSLionel Sambuc !getLangOpts().CPlusPlus11) { 519*f4a2713aSLionel Sambuc // C++03 [basic.lookup.classref]p4: 520*f4a2713aSLionel Sambuc // [...] If the name is found in both contexts, the 521*f4a2713aSLionel Sambuc // class-name-or-namespace-name shall refer to the same entity. 522*f4a2713aSLionel Sambuc // 523*f4a2713aSLionel Sambuc // We already found the name in the scope of the object. Now, look 524*f4a2713aSLionel Sambuc // into the current scope (the scope of the postfix-expression) to 525*f4a2713aSLionel Sambuc // see if we can find the same name there. As above, if there is no 526*f4a2713aSLionel Sambuc // scope, reconstruct the result from the template instantiation itself. 527*f4a2713aSLionel Sambuc // 528*f4a2713aSLionel Sambuc // Note that C++11 does *not* perform this redundant lookup. 529*f4a2713aSLionel Sambuc NamedDecl *OuterDecl; 530*f4a2713aSLionel Sambuc if (S) { 531*f4a2713aSLionel Sambuc LookupResult FoundOuter(*this, &Identifier, IdentifierLoc, 532*f4a2713aSLionel Sambuc LookupNestedNameSpecifierName); 533*f4a2713aSLionel Sambuc LookupName(FoundOuter, S); 534*f4a2713aSLionel Sambuc OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 535*f4a2713aSLionel Sambuc } else 536*f4a2713aSLionel Sambuc OuterDecl = ScopeLookupResult; 537*f4a2713aSLionel Sambuc 538*f4a2713aSLionel Sambuc if (isAcceptableNestedNameSpecifier(OuterDecl) && 539*f4a2713aSLionel Sambuc OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 540*f4a2713aSLionel Sambuc (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 541*f4a2713aSLionel Sambuc !Context.hasSameType( 542*f4a2713aSLionel Sambuc Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 543*f4a2713aSLionel Sambuc Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 544*f4a2713aSLionel Sambuc if (ErrorRecoveryLookup) 545*f4a2713aSLionel Sambuc return true; 546*f4a2713aSLionel Sambuc 547*f4a2713aSLionel Sambuc Diag(IdentifierLoc, 548*f4a2713aSLionel Sambuc diag::err_nested_name_member_ref_lookup_ambiguous) 549*f4a2713aSLionel Sambuc << &Identifier; 550*f4a2713aSLionel Sambuc Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 551*f4a2713aSLionel Sambuc << ObjectType; 552*f4a2713aSLionel Sambuc Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 553*f4a2713aSLionel Sambuc 554*f4a2713aSLionel Sambuc // Fall through so that we'll pick the name we found in the object 555*f4a2713aSLionel Sambuc // type, since that's probably what the user wanted anyway. 556*f4a2713aSLionel Sambuc } 557*f4a2713aSLionel Sambuc } 558*f4a2713aSLionel Sambuc 559*f4a2713aSLionel Sambuc // If we're just performing this lookup for error-recovery purposes, 560*f4a2713aSLionel Sambuc // don't extend the nested-name-specifier. Just return now. 561*f4a2713aSLionel Sambuc if (ErrorRecoveryLookup) 562*f4a2713aSLionel Sambuc return false; 563*f4a2713aSLionel Sambuc 564*f4a2713aSLionel Sambuc if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) { 565*f4a2713aSLionel Sambuc SS.Extend(Context, Namespace, IdentifierLoc, CCLoc); 566*f4a2713aSLionel Sambuc return false; 567*f4a2713aSLionel Sambuc } 568*f4a2713aSLionel Sambuc 569*f4a2713aSLionel Sambuc if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) { 570*f4a2713aSLionel Sambuc SS.Extend(Context, Alias, IdentifierLoc, CCLoc); 571*f4a2713aSLionel Sambuc return false; 572*f4a2713aSLionel Sambuc } 573*f4a2713aSLionel Sambuc 574*f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 575*f4a2713aSLionel Sambuc TypeLocBuilder TLB; 576*f4a2713aSLionel Sambuc if (isa<InjectedClassNameType>(T)) { 577*f4a2713aSLionel Sambuc InjectedClassNameTypeLoc InjectedTL 578*f4a2713aSLionel Sambuc = TLB.push<InjectedClassNameTypeLoc>(T); 579*f4a2713aSLionel Sambuc InjectedTL.setNameLoc(IdentifierLoc); 580*f4a2713aSLionel Sambuc } else if (isa<RecordType>(T)) { 581*f4a2713aSLionel Sambuc RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T); 582*f4a2713aSLionel Sambuc RecordTL.setNameLoc(IdentifierLoc); 583*f4a2713aSLionel Sambuc } else if (isa<TypedefType>(T)) { 584*f4a2713aSLionel Sambuc TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T); 585*f4a2713aSLionel Sambuc TypedefTL.setNameLoc(IdentifierLoc); 586*f4a2713aSLionel Sambuc } else if (isa<EnumType>(T)) { 587*f4a2713aSLionel Sambuc EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T); 588*f4a2713aSLionel Sambuc EnumTL.setNameLoc(IdentifierLoc); 589*f4a2713aSLionel Sambuc } else if (isa<TemplateTypeParmType>(T)) { 590*f4a2713aSLionel Sambuc TemplateTypeParmTypeLoc TemplateTypeTL 591*f4a2713aSLionel Sambuc = TLB.push<TemplateTypeParmTypeLoc>(T); 592*f4a2713aSLionel Sambuc TemplateTypeTL.setNameLoc(IdentifierLoc); 593*f4a2713aSLionel Sambuc } else if (isa<UnresolvedUsingType>(T)) { 594*f4a2713aSLionel Sambuc UnresolvedUsingTypeLoc UnresolvedTL 595*f4a2713aSLionel Sambuc = TLB.push<UnresolvedUsingTypeLoc>(T); 596*f4a2713aSLionel Sambuc UnresolvedTL.setNameLoc(IdentifierLoc); 597*f4a2713aSLionel Sambuc } else if (isa<SubstTemplateTypeParmType>(T)) { 598*f4a2713aSLionel Sambuc SubstTemplateTypeParmTypeLoc TL 599*f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmTypeLoc>(T); 600*f4a2713aSLionel Sambuc TL.setNameLoc(IdentifierLoc); 601*f4a2713aSLionel Sambuc } else if (isa<SubstTemplateTypeParmPackType>(T)) { 602*f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypeLoc TL 603*f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T); 604*f4a2713aSLionel Sambuc TL.setNameLoc(IdentifierLoc); 605*f4a2713aSLionel Sambuc } else { 606*f4a2713aSLionel Sambuc llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier"); 607*f4a2713aSLionel Sambuc } 608*f4a2713aSLionel Sambuc 609*f4a2713aSLionel Sambuc if (T->isEnumeralType()) 610*f4a2713aSLionel Sambuc Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec); 611*f4a2713aSLionel Sambuc 612*f4a2713aSLionel Sambuc SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 613*f4a2713aSLionel Sambuc CCLoc); 614*f4a2713aSLionel Sambuc return false; 615*f4a2713aSLionel Sambuc } 616*f4a2713aSLionel Sambuc 617*f4a2713aSLionel Sambuc // Otherwise, we have an error case. If we don't want diagnostics, just 618*f4a2713aSLionel Sambuc // return an error now. 619*f4a2713aSLionel Sambuc if (ErrorRecoveryLookup) 620*f4a2713aSLionel Sambuc return true; 621*f4a2713aSLionel Sambuc 622*f4a2713aSLionel Sambuc // If we didn't find anything during our lookup, try again with 623*f4a2713aSLionel Sambuc // ordinary name lookup, which can help us produce better error 624*f4a2713aSLionel Sambuc // messages. 625*f4a2713aSLionel Sambuc if (Found.empty()) { 626*f4a2713aSLionel Sambuc Found.clear(LookupOrdinaryName); 627*f4a2713aSLionel Sambuc LookupName(Found, S); 628*f4a2713aSLionel Sambuc } 629*f4a2713aSLionel Sambuc 630*f4a2713aSLionel Sambuc // In Microsoft mode, if we are within a templated function and we can't 631*f4a2713aSLionel Sambuc // resolve Identifier, then extend the SS with Identifier. This will have 632*f4a2713aSLionel Sambuc // the effect of resolving Identifier during template instantiation. 633*f4a2713aSLionel Sambuc // The goal is to be able to resolve a function call whose 634*f4a2713aSLionel Sambuc // nested-name-specifier is located inside a dependent base class. 635*f4a2713aSLionel Sambuc // Example: 636*f4a2713aSLionel Sambuc // 637*f4a2713aSLionel Sambuc // class C { 638*f4a2713aSLionel Sambuc // public: 639*f4a2713aSLionel Sambuc // static void foo2() { } 640*f4a2713aSLionel Sambuc // }; 641*f4a2713aSLionel Sambuc // template <class T> class A { public: typedef C D; }; 642*f4a2713aSLionel Sambuc // 643*f4a2713aSLionel Sambuc // template <class T> class B : public A<T> { 644*f4a2713aSLionel Sambuc // public: 645*f4a2713aSLionel Sambuc // void foo() { D::foo2(); } 646*f4a2713aSLionel Sambuc // }; 647*f4a2713aSLionel Sambuc if (getLangOpts().MicrosoftMode) { 648*f4a2713aSLionel Sambuc DeclContext *DC = LookupCtx ? LookupCtx : CurContext; 649*f4a2713aSLionel Sambuc if (DC->isDependentContext() && DC->isFunctionOrMethod()) { 650*f4a2713aSLionel Sambuc SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); 651*f4a2713aSLionel Sambuc return false; 652*f4a2713aSLionel Sambuc } 653*f4a2713aSLionel Sambuc } 654*f4a2713aSLionel Sambuc 655*f4a2713aSLionel Sambuc unsigned DiagID; 656*f4a2713aSLionel Sambuc if (!Found.empty()) 657*f4a2713aSLionel Sambuc DiagID = diag::err_expected_class_or_namespace; 658*f4a2713aSLionel Sambuc else if (SS.isSet()) { 659*f4a2713aSLionel Sambuc Diag(IdentifierLoc, diag::err_no_member) 660*f4a2713aSLionel Sambuc << &Identifier << LookupCtx << SS.getRange(); 661*f4a2713aSLionel Sambuc return true; 662*f4a2713aSLionel Sambuc } else 663*f4a2713aSLionel Sambuc DiagID = diag::err_undeclared_var_use; 664*f4a2713aSLionel Sambuc 665*f4a2713aSLionel Sambuc if (SS.isSet()) 666*f4a2713aSLionel Sambuc Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange(); 667*f4a2713aSLionel Sambuc else 668*f4a2713aSLionel Sambuc Diag(IdentifierLoc, DiagID) << &Identifier; 669*f4a2713aSLionel Sambuc 670*f4a2713aSLionel Sambuc return true; 671*f4a2713aSLionel Sambuc } 672*f4a2713aSLionel Sambuc 673*f4a2713aSLionel Sambuc bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 674*f4a2713aSLionel Sambuc IdentifierInfo &Identifier, 675*f4a2713aSLionel Sambuc SourceLocation IdentifierLoc, 676*f4a2713aSLionel Sambuc SourceLocation CCLoc, 677*f4a2713aSLionel Sambuc ParsedType ObjectType, 678*f4a2713aSLionel Sambuc bool EnteringContext, 679*f4a2713aSLionel Sambuc CXXScopeSpec &SS) { 680*f4a2713aSLionel Sambuc if (SS.isInvalid()) 681*f4a2713aSLionel Sambuc return true; 682*f4a2713aSLionel Sambuc 683*f4a2713aSLionel Sambuc return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc, 684*f4a2713aSLionel Sambuc GetTypeFromParser(ObjectType), 685*f4a2713aSLionel Sambuc EnteringContext, SS, 686*f4a2713aSLionel Sambuc /*ScopeLookupResult=*/0, false); 687*f4a2713aSLionel Sambuc } 688*f4a2713aSLionel Sambuc 689*f4a2713aSLionel Sambuc bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS, 690*f4a2713aSLionel Sambuc const DeclSpec &DS, 691*f4a2713aSLionel Sambuc SourceLocation ColonColonLoc) { 692*f4a2713aSLionel Sambuc if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) 693*f4a2713aSLionel Sambuc return true; 694*f4a2713aSLionel Sambuc 695*f4a2713aSLionel Sambuc assert(DS.getTypeSpecType() == DeclSpec::TST_decltype); 696*f4a2713aSLionel Sambuc 697*f4a2713aSLionel Sambuc QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); 698*f4a2713aSLionel Sambuc if (!T->isDependentType() && !T->getAs<TagType>()) { 699*f4a2713aSLionel Sambuc Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class) 700*f4a2713aSLionel Sambuc << T << getLangOpts().CPlusPlus; 701*f4a2713aSLionel Sambuc return true; 702*f4a2713aSLionel Sambuc } 703*f4a2713aSLionel Sambuc 704*f4a2713aSLionel Sambuc TypeLocBuilder TLB; 705*f4a2713aSLionel Sambuc DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); 706*f4a2713aSLionel Sambuc DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); 707*f4a2713aSLionel Sambuc SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 708*f4a2713aSLionel Sambuc ColonColonLoc); 709*f4a2713aSLionel Sambuc return false; 710*f4a2713aSLionel Sambuc } 711*f4a2713aSLionel Sambuc 712*f4a2713aSLionel Sambuc /// IsInvalidUnlessNestedName - This method is used for error recovery 713*f4a2713aSLionel Sambuc /// purposes to determine whether the specified identifier is only valid as 714*f4a2713aSLionel Sambuc /// a nested name specifier, for example a namespace name. It is 715*f4a2713aSLionel Sambuc /// conservatively correct to always return false from this method. 716*f4a2713aSLionel Sambuc /// 717*f4a2713aSLionel Sambuc /// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. 718*f4a2713aSLionel Sambuc bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 719*f4a2713aSLionel Sambuc IdentifierInfo &Identifier, 720*f4a2713aSLionel Sambuc SourceLocation IdentifierLoc, 721*f4a2713aSLionel Sambuc SourceLocation ColonLoc, 722*f4a2713aSLionel Sambuc ParsedType ObjectType, 723*f4a2713aSLionel Sambuc bool EnteringContext) { 724*f4a2713aSLionel Sambuc if (SS.isInvalid()) 725*f4a2713aSLionel Sambuc return false; 726*f4a2713aSLionel Sambuc 727*f4a2713aSLionel Sambuc return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc, 728*f4a2713aSLionel Sambuc GetTypeFromParser(ObjectType), 729*f4a2713aSLionel Sambuc EnteringContext, SS, 730*f4a2713aSLionel Sambuc /*ScopeLookupResult=*/0, true); 731*f4a2713aSLionel Sambuc } 732*f4a2713aSLionel Sambuc 733*f4a2713aSLionel Sambuc bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 734*f4a2713aSLionel Sambuc CXXScopeSpec &SS, 735*f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc, 736*f4a2713aSLionel Sambuc TemplateTy Template, 737*f4a2713aSLionel Sambuc SourceLocation TemplateNameLoc, 738*f4a2713aSLionel Sambuc SourceLocation LAngleLoc, 739*f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsIn, 740*f4a2713aSLionel Sambuc SourceLocation RAngleLoc, 741*f4a2713aSLionel Sambuc SourceLocation CCLoc, 742*f4a2713aSLionel Sambuc bool EnteringContext) { 743*f4a2713aSLionel Sambuc if (SS.isInvalid()) 744*f4a2713aSLionel Sambuc return true; 745*f4a2713aSLionel Sambuc 746*f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format. 747*f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 748*f4a2713aSLionel Sambuc translateTemplateArguments(TemplateArgsIn, TemplateArgs); 749*f4a2713aSLionel Sambuc 750*f4a2713aSLionel Sambuc if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){ 751*f4a2713aSLionel Sambuc // Handle a dependent template specialization for which we cannot resolve 752*f4a2713aSLionel Sambuc // the template name. 753*f4a2713aSLionel Sambuc assert(DTN->getQualifier() == SS.getScopeRep()); 754*f4a2713aSLionel Sambuc QualType T = Context.getDependentTemplateSpecializationType(ETK_None, 755*f4a2713aSLionel Sambuc DTN->getQualifier(), 756*f4a2713aSLionel Sambuc DTN->getIdentifier(), 757*f4a2713aSLionel Sambuc TemplateArgs); 758*f4a2713aSLionel Sambuc 759*f4a2713aSLionel Sambuc // Create source-location information for this type. 760*f4a2713aSLionel Sambuc TypeLocBuilder Builder; 761*f4a2713aSLionel Sambuc DependentTemplateSpecializationTypeLoc SpecTL 762*f4a2713aSLionel Sambuc = Builder.push<DependentTemplateSpecializationTypeLoc>(T); 763*f4a2713aSLionel Sambuc SpecTL.setElaboratedKeywordLoc(SourceLocation()); 764*f4a2713aSLionel Sambuc SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); 765*f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 766*f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateNameLoc); 767*f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc); 768*f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc); 769*f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 770*f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 771*f4a2713aSLionel Sambuc 772*f4a2713aSLionel Sambuc SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 773*f4a2713aSLionel Sambuc CCLoc); 774*f4a2713aSLionel Sambuc return false; 775*f4a2713aSLionel Sambuc } 776*f4a2713aSLionel Sambuc 777*f4a2713aSLionel Sambuc 778*f4a2713aSLionel Sambuc if (Template.get().getAsOverloadedTemplate() || 779*f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) { 780*f4a2713aSLionel Sambuc SourceRange R(TemplateNameLoc, RAngleLoc); 781*f4a2713aSLionel Sambuc if (SS.getRange().isValid()) 782*f4a2713aSLionel Sambuc R.setBegin(SS.getRange().getBegin()); 783*f4a2713aSLionel Sambuc 784*f4a2713aSLionel Sambuc Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier) 785*f4a2713aSLionel Sambuc << Template.get() << R; 786*f4a2713aSLionel Sambuc NoteAllFoundTemplates(Template.get()); 787*f4a2713aSLionel Sambuc return true; 788*f4a2713aSLionel Sambuc } 789*f4a2713aSLionel Sambuc 790*f4a2713aSLionel Sambuc // We were able to resolve the template name to an actual template. 791*f4a2713aSLionel Sambuc // Build an appropriate nested-name-specifier. 792*f4a2713aSLionel Sambuc QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc, 793*f4a2713aSLionel Sambuc TemplateArgs); 794*f4a2713aSLionel Sambuc if (T.isNull()) 795*f4a2713aSLionel Sambuc return true; 796*f4a2713aSLionel Sambuc 797*f4a2713aSLionel Sambuc // Alias template specializations can produce types which are not valid 798*f4a2713aSLionel Sambuc // nested name specifiers. 799*f4a2713aSLionel Sambuc if (!T->isDependentType() && !T->getAs<TagType>()) { 800*f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; 801*f4a2713aSLionel Sambuc NoteAllFoundTemplates(Template.get()); 802*f4a2713aSLionel Sambuc return true; 803*f4a2713aSLionel Sambuc } 804*f4a2713aSLionel Sambuc 805*f4a2713aSLionel Sambuc // Provide source-location information for the template specialization type. 806*f4a2713aSLionel Sambuc TypeLocBuilder Builder; 807*f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc SpecTL 808*f4a2713aSLionel Sambuc = Builder.push<TemplateSpecializationTypeLoc>(T); 809*f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 810*f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateNameLoc); 811*f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc); 812*f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc); 813*f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 814*f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 815*f4a2713aSLionel Sambuc 816*f4a2713aSLionel Sambuc 817*f4a2713aSLionel Sambuc SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 818*f4a2713aSLionel Sambuc CCLoc); 819*f4a2713aSLionel Sambuc return false; 820*f4a2713aSLionel Sambuc } 821*f4a2713aSLionel Sambuc 822*f4a2713aSLionel Sambuc namespace { 823*f4a2713aSLionel Sambuc /// \brief A structure that stores a nested-name-specifier annotation, 824*f4a2713aSLionel Sambuc /// including both the nested-name-specifier 825*f4a2713aSLionel Sambuc struct NestedNameSpecifierAnnotation { 826*f4a2713aSLionel Sambuc NestedNameSpecifier *NNS; 827*f4a2713aSLionel Sambuc }; 828*f4a2713aSLionel Sambuc } 829*f4a2713aSLionel Sambuc 830*f4a2713aSLionel Sambuc void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { 831*f4a2713aSLionel Sambuc if (SS.isEmpty() || SS.isInvalid()) 832*f4a2713aSLionel Sambuc return 0; 833*f4a2713aSLionel Sambuc 834*f4a2713aSLionel Sambuc void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) + 835*f4a2713aSLionel Sambuc SS.location_size()), 836*f4a2713aSLionel Sambuc llvm::alignOf<NestedNameSpecifierAnnotation>()); 837*f4a2713aSLionel Sambuc NestedNameSpecifierAnnotation *Annotation 838*f4a2713aSLionel Sambuc = new (Mem) NestedNameSpecifierAnnotation; 839*f4a2713aSLionel Sambuc Annotation->NNS = SS.getScopeRep(); 840*f4a2713aSLionel Sambuc memcpy(Annotation + 1, SS.location_data(), SS.location_size()); 841*f4a2713aSLionel Sambuc return Annotation; 842*f4a2713aSLionel Sambuc } 843*f4a2713aSLionel Sambuc 844*f4a2713aSLionel Sambuc void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, 845*f4a2713aSLionel Sambuc SourceRange AnnotationRange, 846*f4a2713aSLionel Sambuc CXXScopeSpec &SS) { 847*f4a2713aSLionel Sambuc if (!AnnotationPtr) { 848*f4a2713aSLionel Sambuc SS.SetInvalid(AnnotationRange); 849*f4a2713aSLionel Sambuc return; 850*f4a2713aSLionel Sambuc } 851*f4a2713aSLionel Sambuc 852*f4a2713aSLionel Sambuc NestedNameSpecifierAnnotation *Annotation 853*f4a2713aSLionel Sambuc = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); 854*f4a2713aSLionel Sambuc SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); 855*f4a2713aSLionel Sambuc } 856*f4a2713aSLionel Sambuc 857*f4a2713aSLionel Sambuc bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 858*f4a2713aSLionel Sambuc assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 859*f4a2713aSLionel Sambuc 860*f4a2713aSLionel Sambuc NestedNameSpecifier *Qualifier = SS.getScopeRep(); 861*f4a2713aSLionel Sambuc 862*f4a2713aSLionel Sambuc // There are only two places a well-formed program may qualify a 863*f4a2713aSLionel Sambuc // declarator: first, when defining a namespace or class member 864*f4a2713aSLionel Sambuc // out-of-line, and second, when naming an explicitly-qualified 865*f4a2713aSLionel Sambuc // friend function. The latter case is governed by 866*f4a2713aSLionel Sambuc // C++03 [basic.lookup.unqual]p10: 867*f4a2713aSLionel Sambuc // In a friend declaration naming a member function, a name used 868*f4a2713aSLionel Sambuc // in the function declarator and not part of a template-argument 869*f4a2713aSLionel Sambuc // in a template-id is first looked up in the scope of the member 870*f4a2713aSLionel Sambuc // function's class. If it is not found, or if the name is part of 871*f4a2713aSLionel Sambuc // a template-argument in a template-id, the look up is as 872*f4a2713aSLionel Sambuc // described for unqualified names in the definition of the class 873*f4a2713aSLionel Sambuc // granting friendship. 874*f4a2713aSLionel Sambuc // i.e. we don't push a scope unless it's a class member. 875*f4a2713aSLionel Sambuc 876*f4a2713aSLionel Sambuc switch (Qualifier->getKind()) { 877*f4a2713aSLionel Sambuc case NestedNameSpecifier::Global: 878*f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace: 879*f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias: 880*f4a2713aSLionel Sambuc // These are always namespace scopes. We never want to enter a 881*f4a2713aSLionel Sambuc // namespace scope from anything but a file context. 882*f4a2713aSLionel Sambuc return CurContext->getRedeclContext()->isFileContext(); 883*f4a2713aSLionel Sambuc 884*f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier: 885*f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec: 886*f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate: 887*f4a2713aSLionel Sambuc // These are never namespace scopes. 888*f4a2713aSLionel Sambuc return true; 889*f4a2713aSLionel Sambuc } 890*f4a2713aSLionel Sambuc 891*f4a2713aSLionel Sambuc llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 892*f4a2713aSLionel Sambuc } 893*f4a2713aSLionel Sambuc 894*f4a2713aSLionel Sambuc /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 895*f4a2713aSLionel Sambuc /// scope or nested-name-specifier) is parsed, part of a declarator-id. 896*f4a2713aSLionel Sambuc /// After this method is called, according to [C++ 3.4.3p3], names should be 897*f4a2713aSLionel Sambuc /// looked up in the declarator-id's scope, until the declarator is parsed and 898*f4a2713aSLionel Sambuc /// ActOnCXXExitDeclaratorScope is called. 899*f4a2713aSLionel Sambuc /// The 'SS' should be a non-empty valid CXXScopeSpec. 900*f4a2713aSLionel Sambuc bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 901*f4a2713aSLionel Sambuc assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 902*f4a2713aSLionel Sambuc 903*f4a2713aSLionel Sambuc if (SS.isInvalid()) return true; 904*f4a2713aSLionel Sambuc 905*f4a2713aSLionel Sambuc DeclContext *DC = computeDeclContext(SS, true); 906*f4a2713aSLionel Sambuc if (!DC) return true; 907*f4a2713aSLionel Sambuc 908*f4a2713aSLionel Sambuc // Before we enter a declarator's context, we need to make sure that 909*f4a2713aSLionel Sambuc // it is a complete declaration context. 910*f4a2713aSLionel Sambuc if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 911*f4a2713aSLionel Sambuc return true; 912*f4a2713aSLionel Sambuc 913*f4a2713aSLionel Sambuc EnterDeclaratorContext(S, DC); 914*f4a2713aSLionel Sambuc 915*f4a2713aSLionel Sambuc // Rebuild the nested name specifier for the new scope. 916*f4a2713aSLionel Sambuc if (DC->isDependentContext()) 917*f4a2713aSLionel Sambuc RebuildNestedNameSpecifierInCurrentInstantiation(SS); 918*f4a2713aSLionel Sambuc 919*f4a2713aSLionel Sambuc return false; 920*f4a2713aSLionel Sambuc } 921*f4a2713aSLionel Sambuc 922*f4a2713aSLionel Sambuc /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 923*f4a2713aSLionel Sambuc /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 924*f4a2713aSLionel Sambuc /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 925*f4a2713aSLionel Sambuc /// Used to indicate that names should revert to being looked up in the 926*f4a2713aSLionel Sambuc /// defining scope. 927*f4a2713aSLionel Sambuc void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 928*f4a2713aSLionel Sambuc assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 929*f4a2713aSLionel Sambuc if (SS.isInvalid()) 930*f4a2713aSLionel Sambuc return; 931*f4a2713aSLionel Sambuc assert(!SS.isInvalid() && computeDeclContext(SS, true) && 932*f4a2713aSLionel Sambuc "exiting declarator scope we never really entered"); 933*f4a2713aSLionel Sambuc ExitDeclaratorContext(S); 934*f4a2713aSLionel Sambuc } 935