SemaCXXScopeSpec.cpp revision 223017
1243791Sdim//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 2243791Sdim// 3243791Sdim// The LLVM Compiler Infrastructure 4243791Sdim// 5243791Sdim// This file is distributed under the University of Illinois Open Source 6243791Sdim// License. See LICENSE.TXT for details. 7243791Sdim// 8243791Sdim//===----------------------------------------------------------------------===// 9243791Sdim// 10243791Sdim// This file implements C++ semantic analysis for scope specifiers. 11243791Sdim// 12243791Sdim//===----------------------------------------------------------------------===// 13243791Sdim 14243791Sdim#include "clang/Sema/SemaInternal.h" 15243791Sdim#include "clang/Sema/Lookup.h" 16243791Sdim#include "clang/AST/ASTContext.h" 17252723Sdim#include "clang/AST/DeclTemplate.h" 18243791Sdim#include "clang/AST/ExprCXX.h" 19252723Sdim#include "clang/AST/NestedNameSpecifier.h" 20252723Sdim#include "clang/Basic/PartialDiagnostic.h" 21252723Sdim#include "clang/Sema/DeclSpec.h" 22252723Sdim#include "TypeLocBuilder.h" 23243791Sdim#include "llvm/ADT/STLExtras.h" 24243791Sdim#include "llvm/Support/raw_ostream.h" 25243791Sdimusing namespace clang; 26243791Sdim 27243791Sdim/// \brief Find the current instantiation that associated with the given type. 28243791Sdimstatic CXXRecordDecl *getCurrentInstantiationOf(QualType T, 29243791Sdim DeclContext *CurContext) { 30243791Sdim if (T.isNull()) 31243791Sdim return 0; 32243791Sdim 33243791Sdim const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 34243791Sdim if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 35243791Sdim CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 36243791Sdim if (!T->isDependentType()) 37243791Sdim return Record; 38243791Sdim 39243791Sdim // This may be a member of a class template or class template partial 40243791Sdim // specialization. If it's part of the current semantic context, then it's 41243791Sdim // an injected-class-name; 42243791Sdim for (; !CurContext->isFileContext(); CurContext = CurContext->getParent()) 43243791Sdim if (CurContext->Equals(Record)) 44243791Sdim return Record; 45243791Sdim 46243791Sdim return 0; 47243791Sdim } else if (isa<InjectedClassNameType>(Ty)) 48243791Sdim return cast<InjectedClassNameType>(Ty)->getDecl(); 49243791Sdim else 50243791Sdim return 0; 51243791Sdim} 52243791Sdim 53243791Sdim/// \brief Compute the DeclContext that is associated with the given type. 54243791Sdim/// 55243791Sdim/// \param T the type for which we are attempting to find a DeclContext. 56243791Sdim/// 57243791Sdim/// \returns the declaration context represented by the type T, 58243791Sdim/// or NULL if the declaration context cannot be computed (e.g., because it is 59243791Sdim/// dependent and not the current instantiation). 60243791SdimDeclContext *Sema::computeDeclContext(QualType T) { 61243791Sdim if (!T->isDependentType()) 62243791Sdim if (const TagType *Tag = T->getAs<TagType>()) 63243791Sdim return Tag->getDecl(); 64243791Sdim 65243791Sdim return ::getCurrentInstantiationOf(T, CurContext); 66243791Sdim} 67243791Sdim 68243791Sdim/// \brief Compute the DeclContext that is associated with the given 69243791Sdim/// scope specifier. 70243791Sdim/// 71243791Sdim/// \param SS the C++ scope specifier as it appears in the source 72243791Sdim/// 73243791Sdim/// \param EnteringContext when true, we will be entering the context of 74243791Sdim/// this scope specifier, so we can retrieve the declaration context of a 75243791Sdim/// class template or class template partial specialization even if it is 76243791Sdim/// not the current instantiation. 77243791Sdim/// 78243791Sdim/// \returns the declaration context represented by the scope specifier @p SS, 79243791Sdim/// or NULL if the declaration context cannot be computed (e.g., because it is 80243791Sdim/// dependent and not the current instantiation). 81243791SdimDeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 82243791Sdim bool EnteringContext) { 83243791Sdim if (!SS.isSet() || SS.isInvalid()) 84243791Sdim return 0; 85243791Sdim 86243791Sdim NestedNameSpecifier *NNS 87243791Sdim = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 88243791Sdim if (NNS->isDependent()) { 89243791Sdim // If this nested-name-specifier refers to the current 90243791Sdim // instantiation, return its DeclContext. 91243791Sdim if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 92243791Sdim return Record; 93243791Sdim 94243791Sdim if (EnteringContext) { 95243791Sdim const Type *NNSType = NNS->getAsType(); 96243791Sdim if (!NNSType) { 97243791Sdim return 0; 98243791Sdim } 99243791Sdim 100243791Sdim // Look through type alias templates, per C++0x [temp.dep.type]p1. 101243791Sdim NNSType = Context.getCanonicalType(NNSType); 102243791Sdim if (const TemplateSpecializationType *SpecType 103243791Sdim = NNSType->getAs<TemplateSpecializationType>()) { 104243791Sdim // We are entering the context of the nested name specifier, so try to 105243791Sdim // match the nested name specifier to either a primary class template 106243791Sdim // or a class template partial specialization. 107243791Sdim if (ClassTemplateDecl *ClassTemplate 108243791Sdim = dyn_cast_or_null<ClassTemplateDecl>( 109243791Sdim SpecType->getTemplateName().getAsTemplateDecl())) { 110243791Sdim QualType ContextType 111243791Sdim = Context.getCanonicalType(QualType(SpecType, 0)); 112243791Sdim 113243791Sdim // If the type of the nested name specifier is the same as the 114243791Sdim // injected class name of the named class template, we're entering 115252723Sdim // into that class template definition. 116243791Sdim QualType Injected 117252723Sdim = ClassTemplate->getInjectedClassNameSpecialization(); 118252723Sdim if (Context.hasSameType(Injected, ContextType)) 119243791Sdim return ClassTemplate->getTemplatedDecl(); 120252723Sdim 121252723Sdim // If the type of the nested name specifier is the same as the 122252723Sdim // type of one of the class template's class template partial 123252723Sdim // specializations, we're entering into the definition of that 124243791Sdim // class template partial specialization. 125243791Sdim if (ClassTemplatePartialSpecializationDecl *PartialSpec 126243791Sdim = ClassTemplate->findPartialSpecialization(ContextType)) 127243791Sdim return PartialSpec; 128243791Sdim } 129243791Sdim } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 130243791Sdim // The nested name specifier refers to a member of a class template. 131243791Sdim return RecordT->getDecl(); 132243791Sdim } 133243791Sdim } 134243791Sdim 135243791Sdim return 0; 136243791Sdim } 137243791Sdim 138243791Sdim switch (NNS->getKind()) { 139243791Sdim case NestedNameSpecifier::Identifier: 140243791Sdim assert(false && "Dependent nested-name-specifier has no DeclContext"); 141243791Sdim break; 142243791Sdim 143243791Sdim case NestedNameSpecifier::Namespace: 144243791Sdim return NNS->getAsNamespace(); 145243791Sdim 146243791Sdim case NestedNameSpecifier::NamespaceAlias: 147243791Sdim return NNS->getAsNamespaceAlias()->getNamespace(); 148243791Sdim 149243791Sdim case NestedNameSpecifier::TypeSpec: 150243791Sdim case NestedNameSpecifier::TypeSpecWithTemplate: { 151243791Sdim const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 152243791Sdim assert(Tag && "Non-tag type in nested-name-specifier"); 153243791Sdim return Tag->getDecl(); 154243791Sdim } break; 155243791Sdim 156243791Sdim case NestedNameSpecifier::Global: 157243791Sdim return Context.getTranslationUnitDecl(); 158243791Sdim } 159243791Sdim 160243791Sdim // Required to silence a GCC warning. 161243791Sdim return 0; 162243791Sdim} 163243791Sdim 164243791Sdimbool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 165243791Sdim if (!SS.isSet() || SS.isInvalid()) 166243791Sdim return false; 167243791Sdim 168243791Sdim NestedNameSpecifier *NNS 169243791Sdim = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 170243791Sdim return NNS->isDependent(); 171243791Sdim} 172243791Sdim 173252723Sdim// \brief Determine whether this C++ scope specifier refers to an 174252723Sdim// unknown specialization, i.e., a dependent type that is not the 175252723Sdim// current instantiation. 176252723Sdimbool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { 177252723Sdim if (!isDependentScopeSpecifier(SS)) 178252723Sdim return false; 179252723Sdim 180252723Sdim NestedNameSpecifier *NNS 181252723Sdim = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 182252723Sdim return getCurrentInstantiationOf(NNS) == 0; 183252723Sdim} 184252723Sdim 185252723Sdim/// \brief If the given nested name specifier refers to the current 186252723Sdim/// instantiation, return the declaration that corresponds to that 187252723Sdim/// current instantiation (C++0x [temp.dep.type]p1). 188252723Sdim/// 189243791Sdim/// \param NNS a dependent nested name specifier. 190243791SdimCXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 191243791Sdim assert(getLangOptions().CPlusPlus && "Only callable in C++"); 192243791Sdim assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 193243791Sdim 194243791Sdim if (!NNS->getAsType()) 195243791Sdim return 0; 196243791Sdim 197243791Sdim QualType T = QualType(NNS->getAsType(), 0); 198243791Sdim return ::getCurrentInstantiationOf(T, CurContext); 199243791Sdim} 200243791Sdim 201243791Sdim/// \brief Require that the context specified by SS be complete. 202243791Sdim/// 203243791Sdim/// If SS refers to a type, this routine checks whether the type is 204243791Sdim/// complete enough (or can be made complete enough) for name lookup 205243791Sdim/// into the DeclContext. A type that is not yet completed can be 206243791Sdim/// considered "complete enough" if it is a class/struct/union/enum 207243791Sdim/// that is currently being defined. Or, if we have a type that names 208243791Sdim/// a class template specialization that is not a complete type, we 209243791Sdim/// will attempt to instantiate that class template. 210243791Sdimbool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 211243791Sdim DeclContext *DC) { 212243791Sdim assert(DC != 0 && "given null context"); 213243791Sdim 214243791Sdim if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 215243791Sdim // If this is a dependent type, then we consider it complete. 216243791Sdim if (Tag->isDependentContext()) 217243791Sdim return false; 218243791Sdim 219243791Sdim // If we're currently defining this type, then lookup into the 220243791Sdim // type is okay: don't complain that it isn't complete yet. 221243791Sdim const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>(); 222243791Sdim if (TagT && TagT->isBeingDefined()) 223243791Sdim return false; 224243791Sdim 225243791Sdim // The type must be complete. 226243791Sdim if (RequireCompleteType(SS.getRange().getBegin(), 227243791Sdim Context.getTypeDeclType(Tag), 228243791Sdim PDiag(diag::err_incomplete_nested_name_spec) 229243791Sdim << SS.getRange())) { 230243791Sdim SS.SetInvalid(SS.getRange()); 231243791Sdim return true; 232243791Sdim } 233243791Sdim } 234243791Sdim 235243791Sdim return false; 236243791Sdim} 237243791Sdim 238243791Sdimbool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc, 239243791Sdim CXXScopeSpec &SS) { 240243791Sdim SS.MakeGlobal(Context, CCLoc); 241243791Sdim return false; 242243791Sdim} 243243791Sdim 244243791Sdim/// \brief Determines whether the given declaration is an valid acceptable 245243791Sdim/// result for name lookup of a nested-name-specifier. 246243791Sdimbool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) { 247243791Sdim if (!SD) 248243791Sdim return false; 249243791Sdim 250243791Sdim // Namespace and namespace aliases are fine. 251243791Sdim if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) 252243791Sdim return true; 253243791Sdim 254243791Sdim if (!isa<TypeDecl>(SD)) 255243791Sdim return false; 256243791Sdim 257243791Sdim // Determine whether we have a class (or, in C++0x, an enum) or 258243791Sdim // a typedef thereof. If so, build the nested-name-specifier. 259243791Sdim QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 260243791Sdim if (T->isDependentType()) 261243791Sdim return true; 262243791Sdim else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { 263243791Sdim if (TD->getUnderlyingType()->isRecordType() || 264243791Sdim (Context.getLangOptions().CPlusPlus0x && 265243791Sdim TD->getUnderlyingType()->isEnumeralType())) 266243791Sdim return true; 267243791Sdim } else if (isa<RecordDecl>(SD) || 268243791Sdim (Context.getLangOptions().CPlusPlus0x && isa<EnumDecl>(SD))) 269243791Sdim return true; 270243791Sdim 271243791Sdim return false; 272243791Sdim} 273243791Sdim 274243791Sdim/// \brief If the given nested-name-specifier begins with a bare identifier 275243791Sdim/// (e.g., Base::), perform name lookup for that identifier as a 276243791Sdim/// nested-name-specifier within the given scope, and return the result of that 277243791Sdim/// name lookup. 278243791SdimNamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 279243791Sdim if (!S || !NNS) 280243791Sdim return 0; 281243791Sdim 282243791Sdim while (NNS->getPrefix()) 283243791Sdim NNS = NNS->getPrefix(); 284243791Sdim 285243791Sdim if (NNS->getKind() != NestedNameSpecifier::Identifier) 286243791Sdim return 0; 287243791Sdim 288243791Sdim LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 289243791Sdim LookupNestedNameSpecifierName); 290243791Sdim LookupName(Found, S); 291243791Sdim assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 292243791Sdim 293243791Sdim if (!Found.isSingleResult()) 294243791Sdim return 0; 295243791Sdim 296243791Sdim NamedDecl *Result = Found.getFoundDecl(); 297243791Sdim if (isAcceptableNestedNameSpecifier(Result)) 298243791Sdim return Result; 299243791Sdim 300243791Sdim return 0; 301243791Sdim} 302243791Sdim 303243791Sdimbool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, 304243791Sdim SourceLocation IdLoc, 305243791Sdim IdentifierInfo &II, 306243791Sdim ParsedType ObjectTypePtr) { 307243791Sdim QualType ObjectType = GetTypeFromParser(ObjectTypePtr); 308243791Sdim LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 309243791Sdim 310243791Sdim // Determine where to perform name lookup 311243791Sdim DeclContext *LookupCtx = 0; 312243791Sdim bool isDependent = false; 313243791Sdim if (!ObjectType.isNull()) { 314243791Sdim // This nested-name-specifier occurs in a member access expression, e.g., 315243791Sdim // x->B::f, and we are looking into the type of the object. 316243791Sdim assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 317243791Sdim LookupCtx = computeDeclContext(ObjectType); 318243791Sdim isDependent = ObjectType->isDependentType(); 319243791Sdim } else if (SS.isSet()) { 320243791Sdim // This nested-name-specifier occurs after another nested-name-specifier, 321243791Sdim // so long into the context associated with the prior nested-name-specifier. 322243791Sdim LookupCtx = computeDeclContext(SS, false); 323243791Sdim isDependent = isDependentScopeSpecifier(SS); 324243791Sdim Found.setContextRange(SS.getRange()); 325243791Sdim } 326243791Sdim 327243791Sdim if (LookupCtx) { 328243791Sdim // Perform "qualified" name lookup into the declaration context we 329243791Sdim // computed, which is either the type of the base of a member access 330243791Sdim // expression or the declaration context associated with a prior 331243791Sdim // nested-name-specifier. 332243791Sdim 333243791Sdim // The declaration context must be complete. 334243791Sdim if (!LookupCtx->isDependentContext() && 335243791Sdim RequireCompleteDeclContext(SS, LookupCtx)) 336243791Sdim return false; 337243791Sdim 338243791Sdim LookupQualifiedName(Found, LookupCtx); 339243791Sdim } else if (isDependent) { 340243791Sdim return false; 341243791Sdim } else { 342243791Sdim LookupName(Found, S); 343243791Sdim } 344243791Sdim Found.suppressDiagnostics(); 345243791Sdim 346243791Sdim if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 347243791Sdim return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); 348243791Sdim 349243791Sdim return false; 350243791Sdim} 351243791Sdim 352243791Sdim/// \brief Build a new nested-name-specifier for "identifier::", as described 353243791Sdim/// by ActOnCXXNestedNameSpecifier. 354243791Sdim/// 355243791Sdim/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in 356243791Sdim/// that it contains an extra parameter \p ScopeLookupResult, which provides 357243791Sdim/// the result of name lookup within the scope of the nested-name-specifier 358243791Sdim/// that was computed at template definition time. 359243791Sdim/// 360243791Sdim/// If ErrorRecoveryLookup is true, then this call is used to improve error 361243791Sdim/// recovery. This means that it should not emit diagnostics, it should 362243791Sdim/// just return true on failure. It also means it should only return a valid 363243791Sdim/// scope if it *knows* that the result is correct. It should not return in a 364243791Sdim/// dependent context, for example. Nor will it extend \p SS with the scope 365243791Sdim/// specifier. 366243791Sdimbool Sema::BuildCXXNestedNameSpecifier(Scope *S, 367243791Sdim IdentifierInfo &Identifier, 368243791Sdim SourceLocation IdentifierLoc, 369243791Sdim SourceLocation CCLoc, 370243791Sdim QualType ObjectType, 371243791Sdim bool EnteringContext, 372252723Sdim CXXScopeSpec &SS, 373252723Sdim NamedDecl *ScopeLookupResult, 374252723Sdim bool ErrorRecoveryLookup) { 375252723Sdim LookupResult Found(*this, &Identifier, IdentifierLoc, 376252723Sdim LookupNestedNameSpecifierName); 377252723Sdim 378243791Sdim // Determine where to perform name lookup 379252723Sdim DeclContext *LookupCtx = 0; 380252723Sdim bool isDependent = false; 381252723Sdim if (!ObjectType.isNull()) { 382243791Sdim // This nested-name-specifier occurs in a member access expression, e.g., 383252723Sdim // x->B::f, and we are looking into the type of the object. 384252723Sdim assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 385252723Sdim LookupCtx = computeDeclContext(ObjectType); 386243791Sdim isDependent = ObjectType->isDependentType(); 387252723Sdim } else if (SS.isSet()) { 388252723Sdim // This nested-name-specifier occurs after another nested-name-specifier, 389243791Sdim // so look into the context associated with the prior nested-name-specifier. 390252723Sdim LookupCtx = computeDeclContext(SS, EnteringContext); 391243791Sdim isDependent = isDependentScopeSpecifier(SS); 392252723Sdim Found.setContextRange(SS.getRange()); 393252723Sdim } 394243791Sdim 395252723Sdim 396243791Sdim bool ObjectTypeSearchedInScope = false; 397252723Sdim if (LookupCtx) { 398252723Sdim // Perform "qualified" name lookup into the declaration context we 399252723Sdim // computed, which is either the type of the base of a member access 400252723Sdim // expression or the declaration context associated with a prior 401243791Sdim // nested-name-specifier. 402243791Sdim 403252723Sdim // The declaration context must be complete. 404252723Sdim if (!LookupCtx->isDependentContext() && 405252723Sdim RequireCompleteDeclContext(SS, LookupCtx)) 406243791Sdim return true; 407243791Sdim 408252723Sdim LookupQualifiedName(Found, LookupCtx); 409252723Sdim 410252723Sdim if (!ObjectType.isNull() && Found.empty()) { 411243791Sdim // C++ [basic.lookup.classref]p4: 412243791Sdim // If the id-expression in a class member access is a qualified-id of 413252723Sdim // the form 414252723Sdim // 415252723Sdim // class-name-or-namespace-name::... 416252723Sdim // 417252723Sdim // the class-name-or-namespace-name following the . or -> operator is 418252723Sdim // looked up both in the context of the entire postfix-expression and in 419243791Sdim // the scope of the class of the object expression. If the name is found 420243791Sdim // only in the scope of the class of the object expression, the name 421252723Sdim // shall refer to a class-name. If the name is found only in the 422252723Sdim // context of the entire postfix-expression, the name shall refer to a 423252723Sdim // class-name or namespace-name. [...] 424243791Sdim // 425252723Sdim // Qualified name lookup into a class will not find a namespace-name, 426243791Sdim // so we do not need to diagnose that case specifically. However, 427243791Sdim // this qualified name lookup may find nothing. In that case, perform 428243791Sdim // unqualified name lookup in the given scope (if available) or 429243791Sdim // reconstruct the result from when name lookup was performed at template 430243791Sdim // definition time. 431243791Sdim if (S) 432243791Sdim LookupName(Found, S); 433243791Sdim else if (ScopeLookupResult) 434243791Sdim Found.addDecl(ScopeLookupResult); 435243791Sdim 436243791Sdim ObjectTypeSearchedInScope = true; 437243791Sdim } 438243791Sdim } else if (!isDependent) { 439243791Sdim // Perform unqualified name lookup in the current scope. 440252723Sdim LookupName(Found, S); 441243791Sdim } 442252723Sdim 443243791Sdim // If we performed lookup into a dependent context and did not find anything, 444252723Sdim // that's fine: just build a dependent nested-name-specifier. 445243791Sdim if (Found.empty() && isDependent && 446243791Sdim !(LookupCtx && LookupCtx->isRecord() && 447243791Sdim (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || 448243791Sdim !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) { 449243791Sdim // Don't speculate if we're just trying to improve error recovery. 450243791Sdim if (ErrorRecoveryLookup) 451243791Sdim return true; 452243791Sdim 453243791Sdim // We were not able to compute the declaration context for a dependent 454243791Sdim // base object type or prior nested-name-specifier, so this 455243791Sdim // nested-name-specifier refers to an unknown specialization. Just build 456243791Sdim // a dependent nested-name-specifier. 457243791Sdim SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); 458243791Sdim return false; 459243791Sdim } 460243791Sdim 461243791Sdim // FIXME: Deal with ambiguities cleanly. 462243791Sdim 463243791Sdim if (Found.empty() && !ErrorRecoveryLookup) { 464243791Sdim // We haven't found anything, and we're not recovering from a 465243791Sdim // different kind of error, so look for typos. 466243791Sdim DeclarationName Name = Found.getLookupName(); 467243791Sdim if (CorrectTypo(Found, S, &SS, LookupCtx, EnteringContext, 468243791Sdim CTC_NoKeywords) && 469243791Sdim Found.isSingleResult() && 470243791Sdim isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>())) { 471243791Sdim if (LookupCtx) 472252723Sdim Diag(Found.getNameLoc(), diag::err_no_member_suggest) 473252723Sdim << Name << LookupCtx << Found.getLookupName() << SS.getRange() 474252723Sdim << FixItHint::CreateReplacement(Found.getNameLoc(), 475252723Sdim Found.getLookupName().getAsString()); 476252723Sdim else 477252723Sdim Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest) 478252723Sdim << Name << Found.getLookupName() 479252723Sdim << FixItHint::CreateReplacement(Found.getNameLoc(), 480243791Sdim Found.getLookupName().getAsString()); 481243791Sdim 482243791Sdim if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 483252723Sdim Diag(ND->getLocation(), diag::note_previous_decl) 484252723Sdim << ND->getDeclName(); 485243791Sdim } else { 486243791Sdim Found.clear(); 487 Found.setLookupName(&Identifier); 488 } 489 } 490 491 NamedDecl *SD = Found.getAsSingle<NamedDecl>(); 492 if (isAcceptableNestedNameSpecifier(SD)) { 493 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) { 494 // C++ [basic.lookup.classref]p4: 495 // [...] If the name is found in both contexts, the 496 // class-name-or-namespace-name shall refer to the same entity. 497 // 498 // We already found the name in the scope of the object. Now, look 499 // into the current scope (the scope of the postfix-expression) to 500 // see if we can find the same name there. As above, if there is no 501 // scope, reconstruct the result from the template instantiation itself. 502 NamedDecl *OuterDecl; 503 if (S) { 504 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc, 505 LookupNestedNameSpecifierName); 506 LookupName(FoundOuter, S); 507 OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 508 } else 509 OuterDecl = ScopeLookupResult; 510 511 if (isAcceptableNestedNameSpecifier(OuterDecl) && 512 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 513 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 514 !Context.hasSameType( 515 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 516 Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 517 if (ErrorRecoveryLookup) 518 return true; 519 520 Diag(IdentifierLoc, 521 diag::err_nested_name_member_ref_lookup_ambiguous) 522 << &Identifier; 523 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 524 << ObjectType; 525 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 526 527 // Fall through so that we'll pick the name we found in the object 528 // type, since that's probably what the user wanted anyway. 529 } 530 } 531 532 // If we're just performing this lookup for error-recovery purposes, 533 // don't extend the nested-name-specifier. Just return now. 534 if (ErrorRecoveryLookup) 535 return false; 536 537 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) { 538 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc); 539 return false; 540 } 541 542 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) { 543 SS.Extend(Context, Alias, IdentifierLoc, CCLoc); 544 return false; 545 } 546 547 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 548 TypeLocBuilder TLB; 549 if (isa<InjectedClassNameType>(T)) { 550 InjectedClassNameTypeLoc InjectedTL 551 = TLB.push<InjectedClassNameTypeLoc>(T); 552 InjectedTL.setNameLoc(IdentifierLoc); 553 } else if (isa<RecordType>(T)) { 554 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T); 555 RecordTL.setNameLoc(IdentifierLoc); 556 } else if (isa<TypedefType>(T)) { 557 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T); 558 TypedefTL.setNameLoc(IdentifierLoc); 559 } else if (isa<EnumType>(T)) { 560 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T); 561 EnumTL.setNameLoc(IdentifierLoc); 562 } else if (isa<TemplateTypeParmType>(T)) { 563 TemplateTypeParmTypeLoc TemplateTypeTL 564 = TLB.push<TemplateTypeParmTypeLoc>(T); 565 TemplateTypeTL.setNameLoc(IdentifierLoc); 566 } else if (isa<UnresolvedUsingType>(T)) { 567 UnresolvedUsingTypeLoc UnresolvedTL 568 = TLB.push<UnresolvedUsingTypeLoc>(T); 569 UnresolvedTL.setNameLoc(IdentifierLoc); 570 } else if (isa<SubstTemplateTypeParmType>(T)) { 571 SubstTemplateTypeParmTypeLoc TL 572 = TLB.push<SubstTemplateTypeParmTypeLoc>(T); 573 TL.setNameLoc(IdentifierLoc); 574 } else if (isa<SubstTemplateTypeParmPackType>(T)) { 575 SubstTemplateTypeParmPackTypeLoc TL 576 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T); 577 TL.setNameLoc(IdentifierLoc); 578 } else { 579 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier"); 580 } 581 582 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 583 CCLoc); 584 return false; 585 } 586 587 // Otherwise, we have an error case. If we don't want diagnostics, just 588 // return an error now. 589 if (ErrorRecoveryLookup) 590 return true; 591 592 // If we didn't find anything during our lookup, try again with 593 // ordinary name lookup, which can help us produce better error 594 // messages. 595 if (Found.empty()) { 596 Found.clear(LookupOrdinaryName); 597 LookupName(Found, S); 598 } 599 600 unsigned DiagID; 601 if (!Found.empty()) 602 DiagID = diag::err_expected_class_or_namespace; 603 else if (SS.isSet()) { 604 Diag(IdentifierLoc, diag::err_no_member) 605 << &Identifier << LookupCtx << SS.getRange(); 606 return true; 607 } else 608 DiagID = diag::err_undeclared_var_use; 609 610 if (SS.isSet()) 611 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange(); 612 else 613 Diag(IdentifierLoc, DiagID) << &Identifier; 614 615 return true; 616} 617 618bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 619 IdentifierInfo &Identifier, 620 SourceLocation IdentifierLoc, 621 SourceLocation CCLoc, 622 ParsedType ObjectType, 623 bool EnteringContext, 624 CXXScopeSpec &SS) { 625 if (SS.isInvalid()) 626 return true; 627 628 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc, 629 GetTypeFromParser(ObjectType), 630 EnteringContext, SS, 631 /*ScopeLookupResult=*/0, false); 632} 633 634/// IsInvalidUnlessNestedName - This method is used for error recovery 635/// purposes to determine whether the specified identifier is only valid as 636/// a nested name specifier, for example a namespace name. It is 637/// conservatively correct to always return false from this method. 638/// 639/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. 640bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 641 IdentifierInfo &Identifier, 642 SourceLocation IdentifierLoc, 643 SourceLocation ColonLoc, 644 ParsedType ObjectType, 645 bool EnteringContext) { 646 if (SS.isInvalid()) 647 return false; 648 649 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc, 650 GetTypeFromParser(ObjectType), 651 EnteringContext, SS, 652 /*ScopeLookupResult=*/0, true); 653} 654 655bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 656 SourceLocation TemplateLoc, 657 CXXScopeSpec &SS, 658 TemplateTy Template, 659 SourceLocation TemplateNameLoc, 660 SourceLocation LAngleLoc, 661 ASTTemplateArgsPtr TemplateArgsIn, 662 SourceLocation RAngleLoc, 663 SourceLocation CCLoc, 664 bool EnteringContext) { 665 if (SS.isInvalid()) 666 return true; 667 668 // Translate the parser's template argument list in our AST format. 669 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 670 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 671 672 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){ 673 // Handle a dependent template specialization for which we cannot resolve 674 // the template name. 675 assert(DTN->getQualifier() 676 == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); 677 QualType T = Context.getDependentTemplateSpecializationType(ETK_None, 678 DTN->getQualifier(), 679 DTN->getIdentifier(), 680 TemplateArgs); 681 682 // Create source-location information for this type. 683 TypeLocBuilder Builder; 684 DependentTemplateSpecializationTypeLoc SpecTL 685 = Builder.push<DependentTemplateSpecializationTypeLoc>(T); 686 SpecTL.setLAngleLoc(LAngleLoc); 687 SpecTL.setRAngleLoc(RAngleLoc); 688 SpecTL.setKeywordLoc(SourceLocation()); 689 SpecTL.setNameLoc(TemplateNameLoc); 690 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); 691 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 692 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 693 694 SS.Extend(Context, TemplateLoc, Builder.getTypeLocInContext(Context, T), 695 CCLoc); 696 return false; 697 } 698 699 700 if (Template.get().getAsOverloadedTemplate() || 701 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) { 702 SourceRange R(TemplateNameLoc, RAngleLoc); 703 if (SS.getRange().isValid()) 704 R.setBegin(SS.getRange().getBegin()); 705 706 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier) 707 << Template.get() << R; 708 NoteAllFoundTemplates(Template.get()); 709 return true; 710 } 711 712 // We were able to resolve the template name to an actual template. 713 // Build an appropriate nested-name-specifier. 714 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc, 715 TemplateArgs); 716 if (T.isNull()) 717 return true; 718 719 // Alias template specializations can produce types which are not valid 720 // nested name specifiers. 721 if (!T->isDependentType() && !T->getAs<TagType>()) { 722 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; 723 NoteAllFoundTemplates(Template.get()); 724 return true; 725 } 726 727 // Provide source-location information for the template specialization 728 // type. 729 TypeLocBuilder Builder; 730 TemplateSpecializationTypeLoc SpecTL 731 = Builder.push<TemplateSpecializationTypeLoc>(T); 732 733 SpecTL.setLAngleLoc(LAngleLoc); 734 SpecTL.setRAngleLoc(RAngleLoc); 735 SpecTL.setTemplateNameLoc(TemplateNameLoc); 736 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 737 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 738 739 740 SS.Extend(Context, TemplateLoc, Builder.getTypeLocInContext(Context, T), 741 CCLoc); 742 return false; 743} 744 745namespace { 746 /// \brief A structure that stores a nested-name-specifier annotation, 747 /// including both the nested-name-specifier 748 struct NestedNameSpecifierAnnotation { 749 NestedNameSpecifier *NNS; 750 }; 751} 752 753void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { 754 if (SS.isEmpty() || SS.isInvalid()) 755 return 0; 756 757 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) + 758 SS.location_size()), 759 llvm::alignOf<NestedNameSpecifierAnnotation>()); 760 NestedNameSpecifierAnnotation *Annotation 761 = new (Mem) NestedNameSpecifierAnnotation; 762 Annotation->NNS = SS.getScopeRep(); 763 memcpy(Annotation + 1, SS.location_data(), SS.location_size()); 764 return Annotation; 765} 766 767void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, 768 SourceRange AnnotationRange, 769 CXXScopeSpec &SS) { 770 if (!AnnotationPtr) { 771 SS.SetInvalid(AnnotationRange); 772 return; 773 } 774 775 NestedNameSpecifierAnnotation *Annotation 776 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); 777 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); 778} 779 780bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 781 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 782 783 NestedNameSpecifier *Qualifier = 784 static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 785 786 // There are only two places a well-formed program may qualify a 787 // declarator: first, when defining a namespace or class member 788 // out-of-line, and second, when naming an explicitly-qualified 789 // friend function. The latter case is governed by 790 // C++03 [basic.lookup.unqual]p10: 791 // In a friend declaration naming a member function, a name used 792 // in the function declarator and not part of a template-argument 793 // in a template-id is first looked up in the scope of the member 794 // function's class. If it is not found, or if the name is part of 795 // a template-argument in a template-id, the look up is as 796 // described for unqualified names in the definition of the class 797 // granting friendship. 798 // i.e. we don't push a scope unless it's a class member. 799 800 switch (Qualifier->getKind()) { 801 case NestedNameSpecifier::Global: 802 case NestedNameSpecifier::Namespace: 803 case NestedNameSpecifier::NamespaceAlias: 804 // These are always namespace scopes. We never want to enter a 805 // namespace scope from anything but a file context. 806 return CurContext->getRedeclContext()->isFileContext(); 807 808 case NestedNameSpecifier::Identifier: 809 case NestedNameSpecifier::TypeSpec: 810 case NestedNameSpecifier::TypeSpecWithTemplate: 811 // These are never namespace scopes. 812 return true; 813 } 814 815 // Silence bogus warning. 816 return false; 817} 818 819/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 820/// scope or nested-name-specifier) is parsed, part of a declarator-id. 821/// After this method is called, according to [C++ 3.4.3p3], names should be 822/// looked up in the declarator-id's scope, until the declarator is parsed and 823/// ActOnCXXExitDeclaratorScope is called. 824/// The 'SS' should be a non-empty valid CXXScopeSpec. 825bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 826 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 827 828 if (SS.isInvalid()) return true; 829 830 DeclContext *DC = computeDeclContext(SS, true); 831 if (!DC) return true; 832 833 // Before we enter a declarator's context, we need to make sure that 834 // it is a complete declaration context. 835 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 836 return true; 837 838 EnterDeclaratorContext(S, DC); 839 840 // Rebuild the nested name specifier for the new scope. 841 if (DC->isDependentContext()) 842 RebuildNestedNameSpecifierInCurrentInstantiation(SS); 843 844 return false; 845} 846 847/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 848/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 849/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 850/// Used to indicate that names should revert to being looked up in the 851/// defining scope. 852void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 853 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 854 if (SS.isInvalid()) 855 return; 856 assert(!SS.isInvalid() && computeDeclContext(SS, true) && 857 "exiting declarator scope we never really entered"); 858 ExitDeclaratorContext(S); 859} 860