clang API Documentation
00001 //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements C++ semantic analysis for scope specifiers. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Sema/SemaInternal.h" 00015 #include "clang/Sema/Lookup.h" 00016 #include "clang/Sema/Template.h" 00017 #include "clang/AST/ASTContext.h" 00018 #include "clang/AST/DeclTemplate.h" 00019 #include "clang/AST/ExprCXX.h" 00020 #include "clang/AST/NestedNameSpecifier.h" 00021 #include "clang/Basic/PartialDiagnostic.h" 00022 #include "clang/Sema/DeclSpec.h" 00023 #include "TypeLocBuilder.h" 00024 #include "llvm/ADT/STLExtras.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 using namespace clang; 00027 00028 /// \brief Find the current instantiation that associated with the given type. 00029 static CXXRecordDecl *getCurrentInstantiationOf(QualType T, 00030 DeclContext *CurContext) { 00031 if (T.isNull()) 00032 return 0; 00033 00034 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 00035 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 00036 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 00037 if (!T->isDependentType()) 00038 return Record; 00039 00040 // This may be a member of a class template or class template partial 00041 // specialization. If it's part of the current semantic context, then it's 00042 // an injected-class-name; 00043 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent()) 00044 if (CurContext->Equals(Record)) 00045 return Record; 00046 00047 return 0; 00048 } else if (isa<InjectedClassNameType>(Ty)) 00049 return cast<InjectedClassNameType>(Ty)->getDecl(); 00050 else 00051 return 0; 00052 } 00053 00054 /// \brief Compute the DeclContext that is associated with the given type. 00055 /// 00056 /// \param T the type for which we are attempting to find a DeclContext. 00057 /// 00058 /// \returns the declaration context represented by the type T, 00059 /// or NULL if the declaration context cannot be computed (e.g., because it is 00060 /// dependent and not the current instantiation). 00061 DeclContext *Sema::computeDeclContext(QualType T) { 00062 if (!T->isDependentType()) 00063 if (const TagType *Tag = T->getAs<TagType>()) 00064 return Tag->getDecl(); 00065 00066 return ::getCurrentInstantiationOf(T, CurContext); 00067 } 00068 00069 /// \brief Compute the DeclContext that is associated with the given 00070 /// scope specifier. 00071 /// 00072 /// \param SS the C++ scope specifier as it appears in the source 00073 /// 00074 /// \param EnteringContext when true, we will be entering the context of 00075 /// this scope specifier, so we can retrieve the declaration context of a 00076 /// class template or class template partial specialization even if it is 00077 /// not the current instantiation. 00078 /// 00079 /// \returns the declaration context represented by the scope specifier @p SS, 00080 /// or NULL if the declaration context cannot be computed (e.g., because it is 00081 /// dependent and not the current instantiation). 00082 DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 00083 bool EnteringContext) { 00084 if (!SS.isSet() || SS.isInvalid()) 00085 return 0; 00086 00087 NestedNameSpecifier *NNS 00088 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00089 if (NNS->isDependent()) { 00090 // If this nested-name-specifier refers to the current 00091 // instantiation, return its DeclContext. 00092 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 00093 return Record; 00094 00095 if (EnteringContext) { 00096 const Type *NNSType = NNS->getAsType(); 00097 if (!NNSType) { 00098 return 0; 00099 } 00100 00101 // Look through type alias templates, per C++0x [temp.dep.type]p1. 00102 NNSType = Context.getCanonicalType(NNSType); 00103 if (const TemplateSpecializationType *SpecType 00104 = NNSType->getAs<TemplateSpecializationType>()) { 00105 // We are entering the context of the nested name specifier, so try to 00106 // match the nested name specifier to either a primary class template 00107 // or a class template partial specialization. 00108 if (ClassTemplateDecl *ClassTemplate 00109 = dyn_cast_or_null<ClassTemplateDecl>( 00110 SpecType->getTemplateName().getAsTemplateDecl())) { 00111 QualType ContextType 00112 = Context.getCanonicalType(QualType(SpecType, 0)); 00113 00114 // If the type of the nested name specifier is the same as the 00115 // injected class name of the named class template, we're entering 00116 // into that class template definition. 00117 QualType Injected 00118 = ClassTemplate->getInjectedClassNameSpecialization(); 00119 if (Context.hasSameType(Injected, ContextType)) 00120 return ClassTemplate->getTemplatedDecl(); 00121 00122 // If the type of the nested name specifier is the same as the 00123 // type of one of the class template's class template partial 00124 // specializations, we're entering into the definition of that 00125 // class template partial specialization. 00126 if (ClassTemplatePartialSpecializationDecl *PartialSpec 00127 = ClassTemplate->findPartialSpecialization(ContextType)) 00128 return PartialSpec; 00129 } 00130 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 00131 // The nested name specifier refers to a member of a class template. 00132 return RecordT->getDecl(); 00133 } 00134 } 00135 00136 return 0; 00137 } 00138 00139 switch (NNS->getKind()) { 00140 case NestedNameSpecifier::Identifier: 00141 llvm_unreachable("Dependent nested-name-specifier has no DeclContext"); 00142 00143 case NestedNameSpecifier::Namespace: 00144 return NNS->getAsNamespace(); 00145 00146 case NestedNameSpecifier::NamespaceAlias: 00147 return NNS->getAsNamespaceAlias()->getNamespace(); 00148 00149 case NestedNameSpecifier::TypeSpec: 00150 case NestedNameSpecifier::TypeSpecWithTemplate: { 00151 const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 00152 assert(Tag && "Non-tag type in nested-name-specifier"); 00153 return Tag->getDecl(); 00154 } 00155 00156 case NestedNameSpecifier::Global: 00157 return Context.getTranslationUnitDecl(); 00158 } 00159 00160 llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 00161 } 00162 00163 bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 00164 if (!SS.isSet() || SS.isInvalid()) 00165 return false; 00166 00167 NestedNameSpecifier *NNS 00168 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00169 return NNS->isDependent(); 00170 } 00171 00172 // \brief Determine whether this C++ scope specifier refers to an 00173 // unknown specialization, i.e., a dependent type that is not the 00174 // current instantiation. 00175 bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { 00176 if (!isDependentScopeSpecifier(SS)) 00177 return false; 00178 00179 NestedNameSpecifier *NNS 00180 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00181 return getCurrentInstantiationOf(NNS) == 0; 00182 } 00183 00184 /// \brief If the given nested name specifier refers to the current 00185 /// instantiation, return the declaration that corresponds to that 00186 /// current instantiation (C++0x [temp.dep.type]p1). 00187 /// 00188 /// \param NNS a dependent nested name specifier. 00189 CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 00190 assert(getLangOpts().CPlusPlus && "Only callable in C++"); 00191 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 00192 00193 if (!NNS->getAsType()) 00194 return 0; 00195 00196 QualType T = QualType(NNS->getAsType(), 0); 00197 return ::getCurrentInstantiationOf(T, CurContext); 00198 } 00199 00200 /// \brief Require that the context specified by SS be complete. 00201 /// 00202 /// If SS refers to a type, this routine checks whether the type is 00203 /// complete enough (or can be made complete enough) for name lookup 00204 /// into the DeclContext. A type that is not yet completed can be 00205 /// considered "complete enough" if it is a class/struct/union/enum 00206 /// that is currently being defined. Or, if we have a type that names 00207 /// a class template specialization that is not a complete type, we 00208 /// will attempt to instantiate that class template. 00209 bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 00210 DeclContext *DC) { 00211 assert(DC != 0 && "given null context"); 00212 00213 TagDecl *tag = dyn_cast<TagDecl>(DC); 00214 00215 // If this is a dependent type, then we consider it complete. 00216 if (!tag || tag->isDependentContext()) 00217 return false; 00218 00219 // If we're currently defining this type, then lookup into the 00220 // type is okay: don't complain that it isn't complete yet. 00221 QualType type = Context.getTypeDeclType(tag); 00222 const TagType *tagType = type->getAs<TagType>(); 00223 if (tagType && tagType->isBeingDefined()) 00224 return false; 00225 00226 SourceLocation loc = SS.getLastQualifierNameLoc(); 00227 if (loc.isInvalid()) loc = SS.getRange().getBegin(); 00228 00229 // The type must be complete. 00230 if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec, 00231 SS.getRange())) { 00232 SS.SetInvalid(SS.getRange()); 00233 return true; 00234 } 00235 00236 // Fixed enum types are complete, but they aren't valid as scopes 00237 // until we see a definition, so awkwardly pull out this special 00238 // case. 00239 const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType); 00240 if (!enumType || enumType->getDecl()->isCompleteDefinition()) 00241 return false; 00242 00243 // Try to instantiate the definition, if this is a specialization of an 00244 // enumeration temploid. 00245 EnumDecl *ED = enumType->getDecl(); 00246 if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) { 00247 MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo(); 00248 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) { 00249 if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED), 00250 TSK_ImplicitInstantiation)) { 00251 SS.SetInvalid(SS.getRange()); 00252 return true; 00253 } 00254 return false; 00255 } 00256 } 00257 00258 Diag(loc, diag::err_incomplete_nested_name_spec) 00259 << type << SS.getRange(); 00260 SS.SetInvalid(SS.getRange()); 00261 return true; 00262 } 00263 00264 bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc, 00265 CXXScopeSpec &SS) { 00266 SS.MakeGlobal(Context, CCLoc); 00267 return false; 00268 } 00269 00270 /// \brief Determines whether the given declaration is an valid acceptable 00271 /// result for name lookup of a nested-name-specifier. 00272 bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) { 00273 if (!SD) 00274 return false; 00275 00276 // Namespace and namespace aliases are fine. 00277 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) 00278 return true; 00279 00280 if (!isa<TypeDecl>(SD)) 00281 return false; 00282 00283 // Determine whether we have a class (or, in C++11, an enum) or 00284 // a typedef thereof. If so, build the nested-name-specifier. 00285 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 00286 if (T->isDependentType()) 00287 return true; 00288 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { 00289 if (TD->getUnderlyingType()->isRecordType() || 00290 (Context.getLangOpts().CPlusPlus0x && 00291 TD->getUnderlyingType()->isEnumeralType())) 00292 return true; 00293 } else if (isa<RecordDecl>(SD) || 00294 (Context.getLangOpts().CPlusPlus0x && isa<EnumDecl>(SD))) 00295 return true; 00296 00297 return false; 00298 } 00299 00300 /// \brief If the given nested-name-specifier begins with a bare identifier 00301 /// (e.g., Base::), perform name lookup for that identifier as a 00302 /// nested-name-specifier within the given scope, and return the result of that 00303 /// name lookup. 00304 NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 00305 if (!S || !NNS) 00306 return 0; 00307 00308 while (NNS->getPrefix()) 00309 NNS = NNS->getPrefix(); 00310 00311 if (NNS->getKind() != NestedNameSpecifier::Identifier) 00312 return 0; 00313 00314 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 00315 LookupNestedNameSpecifierName); 00316 LookupName(Found, S); 00317 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 00318 00319 if (!Found.isSingleResult()) 00320 return 0; 00321 00322 NamedDecl *Result = Found.getFoundDecl(); 00323 if (isAcceptableNestedNameSpecifier(Result)) 00324 return Result; 00325 00326 return 0; 00327 } 00328 00329 bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, 00330 SourceLocation IdLoc, 00331 IdentifierInfo &II, 00332 ParsedType ObjectTypePtr) { 00333 QualType ObjectType = GetTypeFromParser(ObjectTypePtr); 00334 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 00335 00336 // Determine where to perform name lookup 00337 DeclContext *LookupCtx = 0; 00338 bool isDependent = false; 00339 if (!ObjectType.isNull()) { 00340 // This nested-name-specifier occurs in a member access expression, e.g., 00341 // x->B::f, and we are looking into the type of the object. 00342 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 00343 LookupCtx = computeDeclContext(ObjectType); 00344 isDependent = ObjectType->isDependentType(); 00345 } else if (SS.isSet()) { 00346 // This nested-name-specifier occurs after another nested-name-specifier, 00347 // so long into the context associated with the prior nested-name-specifier. 00348 LookupCtx = computeDeclContext(SS, false); 00349 isDependent = isDependentScopeSpecifier(SS); 00350 Found.setContextRange(SS.getRange()); 00351 } 00352 00353 if (LookupCtx) { 00354 // Perform "qualified" name lookup into the declaration context we 00355 // computed, which is either the type of the base of a member access 00356 // expression or the declaration context associated with a prior 00357 // nested-name-specifier. 00358 00359 // The declaration context must be complete. 00360 if (!LookupCtx->isDependentContext() && 00361 RequireCompleteDeclContext(SS, LookupCtx)) 00362 return false; 00363 00364 LookupQualifiedName(Found, LookupCtx); 00365 } else if (isDependent) { 00366 return false; 00367 } else { 00368 LookupName(Found, S); 00369 } 00370 Found.suppressDiagnostics(); 00371 00372 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 00373 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); 00374 00375 return false; 00376 } 00377 00378 namespace { 00379 00380 // Callback to only accept typo corrections that can be a valid C++ member 00381 // intializer: either a non-static field member or a base class. 00382 class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback { 00383 public: 00384 explicit NestedNameSpecifierValidatorCCC(Sema &SRef) 00385 : SRef(SRef) {} 00386 00387 virtual bool ValidateCandidate(const TypoCorrection &candidate) { 00388 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl()); 00389 } 00390 00391 private: 00392 Sema &SRef; 00393 }; 00394 00395 } 00396 00397 /// \brief Build a new nested-name-specifier for "identifier::", as described 00398 /// by ActOnCXXNestedNameSpecifier. 00399 /// 00400 /// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in 00401 /// that it contains an extra parameter \p ScopeLookupResult, which provides 00402 /// the result of name lookup within the scope of the nested-name-specifier 00403 /// that was computed at template definition time. 00404 /// 00405 /// If ErrorRecoveryLookup is true, then this call is used to improve error 00406 /// recovery. This means that it should not emit diagnostics, it should 00407 /// just return true on failure. It also means it should only return a valid 00408 /// scope if it *knows* that the result is correct. It should not return in a 00409 /// dependent context, for example. Nor will it extend \p SS with the scope 00410 /// specifier. 00411 bool Sema::BuildCXXNestedNameSpecifier(Scope *S, 00412 IdentifierInfo &Identifier, 00413 SourceLocation IdentifierLoc, 00414 SourceLocation CCLoc, 00415 QualType ObjectType, 00416 bool EnteringContext, 00417 CXXScopeSpec &SS, 00418 NamedDecl *ScopeLookupResult, 00419 bool ErrorRecoveryLookup) { 00420 LookupResult Found(*this, &Identifier, IdentifierLoc, 00421 LookupNestedNameSpecifierName); 00422 00423 // Determine where to perform name lookup 00424 DeclContext *LookupCtx = 0; 00425 bool isDependent = false; 00426 if (!ObjectType.isNull()) { 00427 // This nested-name-specifier occurs in a member access expression, e.g., 00428 // x->B::f, and we are looking into the type of the object. 00429 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 00430 LookupCtx = computeDeclContext(ObjectType); 00431 isDependent = ObjectType->isDependentType(); 00432 } else if (SS.isSet()) { 00433 // This nested-name-specifier occurs after another nested-name-specifier, 00434 // so look into the context associated with the prior nested-name-specifier. 00435 LookupCtx = computeDeclContext(SS, EnteringContext); 00436 isDependent = isDependentScopeSpecifier(SS); 00437 Found.setContextRange(SS.getRange()); 00438 } 00439 00440 00441 bool ObjectTypeSearchedInScope = false; 00442 if (LookupCtx) { 00443 // Perform "qualified" name lookup into the declaration context we 00444 // computed, which is either the type of the base of a member access 00445 // expression or the declaration context associated with a prior 00446 // nested-name-specifier. 00447 00448 // The declaration context must be complete. 00449 if (!LookupCtx->isDependentContext() && 00450 RequireCompleteDeclContext(SS, LookupCtx)) 00451 return true; 00452 00453 LookupQualifiedName(Found, LookupCtx); 00454 00455 if (!ObjectType.isNull() && Found.empty()) { 00456 // C++ [basic.lookup.classref]p4: 00457 // If the id-expression in a class member access is a qualified-id of 00458 // the form 00459 // 00460 // class-name-or-namespace-name::... 00461 // 00462 // the class-name-or-namespace-name following the . or -> operator is 00463 // looked up both in the context of the entire postfix-expression and in 00464 // the scope of the class of the object expression. If the name is found 00465 // only in the scope of the class of the object expression, the name 00466 // shall refer to a class-name. If the name is found only in the 00467 // context of the entire postfix-expression, the name shall refer to a 00468 // class-name or namespace-name. [...] 00469 // 00470 // Qualified name lookup into a class will not find a namespace-name, 00471 // so we do not need to diagnose that case specifically. However, 00472 // this qualified name lookup may find nothing. In that case, perform 00473 // unqualified name lookup in the given scope (if available) or 00474 // reconstruct the result from when name lookup was performed at template 00475 // definition time. 00476 if (S) 00477 LookupName(Found, S); 00478 else if (ScopeLookupResult) 00479 Found.addDecl(ScopeLookupResult); 00480 00481 ObjectTypeSearchedInScope = true; 00482 } 00483 } else if (!isDependent) { 00484 // Perform unqualified name lookup in the current scope. 00485 LookupName(Found, S); 00486 } 00487 00488 // If we performed lookup into a dependent context and did not find anything, 00489 // that's fine: just build a dependent nested-name-specifier. 00490 if (Found.empty() && isDependent && 00491 !(LookupCtx && LookupCtx->isRecord() && 00492 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || 00493 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) { 00494 // Don't speculate if we're just trying to improve error recovery. 00495 if (ErrorRecoveryLookup) 00496 return true; 00497 00498 // We were not able to compute the declaration context for a dependent 00499 // base object type or prior nested-name-specifier, so this 00500 // nested-name-specifier refers to an unknown specialization. Just build 00501 // a dependent nested-name-specifier. 00502 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); 00503 return false; 00504 } 00505 00506 // FIXME: Deal with ambiguities cleanly. 00507 00508 if (Found.empty() && !ErrorRecoveryLookup) { 00509 // We haven't found anything, and we're not recovering from a 00510 // different kind of error, so look for typos. 00511 DeclarationName Name = Found.getLookupName(); 00512 NestedNameSpecifierValidatorCCC Validator(*this); 00513 TypoCorrection Corrected; 00514 Found.clear(); 00515 if ((Corrected = CorrectTypo(Found.getLookupNameInfo(), 00516 Found.getLookupKind(), S, &SS, Validator, 00517 LookupCtx, EnteringContext))) { 00518 std::string CorrectedStr(Corrected.getAsString(getLangOpts())); 00519 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); 00520 if (LookupCtx) 00521 Diag(Found.getNameLoc(), diag::err_no_member_suggest) 00522 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() 00523 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); 00524 else 00525 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest) 00526 << Name << CorrectedQuotedStr 00527 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); 00528 00529 if (NamedDecl *ND = Corrected.getCorrectionDecl()) { 00530 Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr; 00531 Found.addDecl(ND); 00532 } 00533 Found.setLookupName(Corrected.getCorrection()); 00534 } else { 00535 Found.setLookupName(&Identifier); 00536 } 00537 } 00538 00539 NamedDecl *SD = Found.getAsSingle<NamedDecl>(); 00540 if (isAcceptableNestedNameSpecifier(SD)) { 00541 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope && 00542 !getLangOpts().CPlusPlus0x) { 00543 // C++03 [basic.lookup.classref]p4: 00544 // [...] If the name is found in both contexts, the 00545 // class-name-or-namespace-name shall refer to the same entity. 00546 // 00547 // We already found the name in the scope of the object. Now, look 00548 // into the current scope (the scope of the postfix-expression) to 00549 // see if we can find the same name there. As above, if there is no 00550 // scope, reconstruct the result from the template instantiation itself. 00551 // 00552 // Note that C++11 does *not* perform this redundant lookup. 00553 NamedDecl *OuterDecl; 00554 if (S) { 00555 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc, 00556 LookupNestedNameSpecifierName); 00557 LookupName(FoundOuter, S); 00558 OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 00559 } else 00560 OuterDecl = ScopeLookupResult; 00561 00562 if (isAcceptableNestedNameSpecifier(OuterDecl) && 00563 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 00564 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 00565 !Context.hasSameType( 00566 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 00567 Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 00568 if (ErrorRecoveryLookup) 00569 return true; 00570 00571 Diag(IdentifierLoc, 00572 diag::err_nested_name_member_ref_lookup_ambiguous) 00573 << &Identifier; 00574 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 00575 << ObjectType; 00576 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 00577 00578 // Fall through so that we'll pick the name we found in the object 00579 // type, since that's probably what the user wanted anyway. 00580 } 00581 } 00582 00583 // If we're just performing this lookup for error-recovery purposes, 00584 // don't extend the nested-name-specifier. Just return now. 00585 if (ErrorRecoveryLookup) 00586 return false; 00587 00588 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) { 00589 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc); 00590 return false; 00591 } 00592 00593 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) { 00594 SS.Extend(Context, Alias, IdentifierLoc, CCLoc); 00595 return false; 00596 } 00597 00598 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 00599 TypeLocBuilder TLB; 00600 if (isa<InjectedClassNameType>(T)) { 00601 InjectedClassNameTypeLoc InjectedTL 00602 = TLB.push<InjectedClassNameTypeLoc>(T); 00603 InjectedTL.setNameLoc(IdentifierLoc); 00604 } else if (isa<RecordType>(T)) { 00605 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T); 00606 RecordTL.setNameLoc(IdentifierLoc); 00607 } else if (isa<TypedefType>(T)) { 00608 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T); 00609 TypedefTL.setNameLoc(IdentifierLoc); 00610 } else if (isa<EnumType>(T)) { 00611 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T); 00612 EnumTL.setNameLoc(IdentifierLoc); 00613 } else if (isa<TemplateTypeParmType>(T)) { 00614 TemplateTypeParmTypeLoc TemplateTypeTL 00615 = TLB.push<TemplateTypeParmTypeLoc>(T); 00616 TemplateTypeTL.setNameLoc(IdentifierLoc); 00617 } else if (isa<UnresolvedUsingType>(T)) { 00618 UnresolvedUsingTypeLoc UnresolvedTL 00619 = TLB.push<UnresolvedUsingTypeLoc>(T); 00620 UnresolvedTL.setNameLoc(IdentifierLoc); 00621 } else if (isa<SubstTemplateTypeParmType>(T)) { 00622 SubstTemplateTypeParmTypeLoc TL 00623 = TLB.push<SubstTemplateTypeParmTypeLoc>(T); 00624 TL.setNameLoc(IdentifierLoc); 00625 } else if (isa<SubstTemplateTypeParmPackType>(T)) { 00626 SubstTemplateTypeParmPackTypeLoc TL 00627 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T); 00628 TL.setNameLoc(IdentifierLoc); 00629 } else { 00630 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier"); 00631 } 00632 00633 if (T->isEnumeralType()) 00634 Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec); 00635 00636 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 00637 CCLoc); 00638 return false; 00639 } 00640 00641 // Otherwise, we have an error case. If we don't want diagnostics, just 00642 // return an error now. 00643 if (ErrorRecoveryLookup) 00644 return true; 00645 00646 // If we didn't find anything during our lookup, try again with 00647 // ordinary name lookup, which can help us produce better error 00648 // messages. 00649 if (Found.empty()) { 00650 Found.clear(LookupOrdinaryName); 00651 LookupName(Found, S); 00652 } 00653 00654 // In Microsoft mode, if we are within a templated function and we can't 00655 // resolve Identifier, then extend the SS with Identifier. This will have 00656 // the effect of resolving Identifier during template instantiation. 00657 // The goal is to be able to resolve a function call whose 00658 // nested-name-specifier is located inside a dependent base class. 00659 // Example: 00660 // 00661 // class C { 00662 // public: 00663 // static void foo2() { } 00664 // }; 00665 // template <class T> class A { public: typedef C D; }; 00666 // 00667 // template <class T> class B : public A<T> { 00668 // public: 00669 // void foo() { D::foo2(); } 00670 // }; 00671 if (getLangOpts().MicrosoftExt) { 00672 DeclContext *DC = LookupCtx ? LookupCtx : CurContext; 00673 if (DC->isDependentContext() && DC->isFunctionOrMethod()) { 00674 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); 00675 return false; 00676 } 00677 } 00678 00679 unsigned DiagID; 00680 if (!Found.empty()) 00681 DiagID = diag::err_expected_class_or_namespace; 00682 else if (SS.isSet()) { 00683 Diag(IdentifierLoc, diag::err_no_member) 00684 << &Identifier << LookupCtx << SS.getRange(); 00685 return true; 00686 } else 00687 DiagID = diag::err_undeclared_var_use; 00688 00689 if (SS.isSet()) 00690 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange(); 00691 else 00692 Diag(IdentifierLoc, DiagID) << &Identifier; 00693 00694 return true; 00695 } 00696 00697 bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 00698 IdentifierInfo &Identifier, 00699 SourceLocation IdentifierLoc, 00700 SourceLocation CCLoc, 00701 ParsedType ObjectType, 00702 bool EnteringContext, 00703 CXXScopeSpec &SS) { 00704 if (SS.isInvalid()) 00705 return true; 00706 00707 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc, 00708 GetTypeFromParser(ObjectType), 00709 EnteringContext, SS, 00710 /*ScopeLookupResult=*/0, false); 00711 } 00712 00713 bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS, 00714 const DeclSpec &DS, 00715 SourceLocation ColonColonLoc) { 00716 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) 00717 return true; 00718 00719 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype); 00720 00721 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); 00722 if (!T->isDependentType() && !T->getAs<TagType>()) { 00723 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class) 00724 << T << getLangOpts().CPlusPlus; 00725 return true; 00726 } 00727 00728 TypeLocBuilder TLB; 00729 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); 00730 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); 00731 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 00732 ColonColonLoc); 00733 return false; 00734 } 00735 00736 /// IsInvalidUnlessNestedName - This method is used for error recovery 00737 /// purposes to determine whether the specified identifier is only valid as 00738 /// a nested name specifier, for example a namespace name. It is 00739 /// conservatively correct to always return false from this method. 00740 /// 00741 /// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. 00742 bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 00743 IdentifierInfo &Identifier, 00744 SourceLocation IdentifierLoc, 00745 SourceLocation ColonLoc, 00746 ParsedType ObjectType, 00747 bool EnteringContext) { 00748 if (SS.isInvalid()) 00749 return false; 00750 00751 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc, 00752 GetTypeFromParser(ObjectType), 00753 EnteringContext, SS, 00754 /*ScopeLookupResult=*/0, true); 00755 } 00756 00757 bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 00758 CXXScopeSpec &SS, 00759 SourceLocation TemplateKWLoc, 00760 TemplateTy Template, 00761 SourceLocation TemplateNameLoc, 00762 SourceLocation LAngleLoc, 00763 ASTTemplateArgsPtr TemplateArgsIn, 00764 SourceLocation RAngleLoc, 00765 SourceLocation CCLoc, 00766 bool EnteringContext) { 00767 if (SS.isInvalid()) 00768 return true; 00769 00770 // Translate the parser's template argument list in our AST format. 00771 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 00772 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 00773 00774 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){ 00775 // Handle a dependent template specialization for which we cannot resolve 00776 // the template name. 00777 assert(DTN->getQualifier() 00778 == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); 00779 QualType T = Context.getDependentTemplateSpecializationType(ETK_None, 00780 DTN->getQualifier(), 00781 DTN->getIdentifier(), 00782 TemplateArgs); 00783 00784 // Create source-location information for this type. 00785 TypeLocBuilder Builder; 00786 DependentTemplateSpecializationTypeLoc SpecTL 00787 = Builder.push<DependentTemplateSpecializationTypeLoc>(T); 00788 SpecTL.setElaboratedKeywordLoc(SourceLocation()); 00789 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); 00790 SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 00791 SpecTL.setTemplateNameLoc(TemplateNameLoc); 00792 SpecTL.setLAngleLoc(LAngleLoc); 00793 SpecTL.setRAngleLoc(RAngleLoc); 00794 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 00795 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 00796 00797 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 00798 CCLoc); 00799 return false; 00800 } 00801 00802 00803 if (Template.get().getAsOverloadedTemplate() || 00804 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) { 00805 SourceRange R(TemplateNameLoc, RAngleLoc); 00806 if (SS.getRange().isValid()) 00807 R.setBegin(SS.getRange().getBegin()); 00808 00809 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier) 00810 << Template.get() << R; 00811 NoteAllFoundTemplates(Template.get()); 00812 return true; 00813 } 00814 00815 // We were able to resolve the template name to an actual template. 00816 // Build an appropriate nested-name-specifier. 00817 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc, 00818 TemplateArgs); 00819 if (T.isNull()) 00820 return true; 00821 00822 // Alias template specializations can produce types which are not valid 00823 // nested name specifiers. 00824 if (!T->isDependentType() && !T->getAs<TagType>()) { 00825 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; 00826 NoteAllFoundTemplates(Template.get()); 00827 return true; 00828 } 00829 00830 // Provide source-location information for the template specialization type. 00831 TypeLocBuilder Builder; 00832 TemplateSpecializationTypeLoc SpecTL 00833 = Builder.push<TemplateSpecializationTypeLoc>(T); 00834 SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 00835 SpecTL.setTemplateNameLoc(TemplateNameLoc); 00836 SpecTL.setLAngleLoc(LAngleLoc); 00837 SpecTL.setRAngleLoc(RAngleLoc); 00838 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 00839 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 00840 00841 00842 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 00843 CCLoc); 00844 return false; 00845 } 00846 00847 namespace { 00848 /// \brief A structure that stores a nested-name-specifier annotation, 00849 /// including both the nested-name-specifier 00850 struct NestedNameSpecifierAnnotation { 00851 NestedNameSpecifier *NNS; 00852 }; 00853 } 00854 00855 void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { 00856 if (SS.isEmpty() || SS.isInvalid()) 00857 return 0; 00858 00859 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) + 00860 SS.location_size()), 00861 llvm::alignOf<NestedNameSpecifierAnnotation>()); 00862 NestedNameSpecifierAnnotation *Annotation 00863 = new (Mem) NestedNameSpecifierAnnotation; 00864 Annotation->NNS = SS.getScopeRep(); 00865 memcpy(Annotation + 1, SS.location_data(), SS.location_size()); 00866 return Annotation; 00867 } 00868 00869 void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, 00870 SourceRange AnnotationRange, 00871 CXXScopeSpec &SS) { 00872 if (!AnnotationPtr) { 00873 SS.SetInvalid(AnnotationRange); 00874 return; 00875 } 00876 00877 NestedNameSpecifierAnnotation *Annotation 00878 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); 00879 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); 00880 } 00881 00882 bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 00883 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 00884 00885 NestedNameSpecifier *Qualifier = 00886 static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 00887 00888 // There are only two places a well-formed program may qualify a 00889 // declarator: first, when defining a namespace or class member 00890 // out-of-line, and second, when naming an explicitly-qualified 00891 // friend function. The latter case is governed by 00892 // C++03 [basic.lookup.unqual]p10: 00893 // In a friend declaration naming a member function, a name used 00894 // in the function declarator and not part of a template-argument 00895 // in a template-id is first looked up in the scope of the member 00896 // function's class. If it is not found, or if the name is part of 00897 // a template-argument in a template-id, the look up is as 00898 // described for unqualified names in the definition of the class 00899 // granting friendship. 00900 // i.e. we don't push a scope unless it's a class member. 00901 00902 switch (Qualifier->getKind()) { 00903 case NestedNameSpecifier::Global: 00904 case NestedNameSpecifier::Namespace: 00905 case NestedNameSpecifier::NamespaceAlias: 00906 // These are always namespace scopes. We never want to enter a 00907 // namespace scope from anything but a file context. 00908 return CurContext->getRedeclContext()->isFileContext(); 00909 00910 case NestedNameSpecifier::Identifier: 00911 case NestedNameSpecifier::TypeSpec: 00912 case NestedNameSpecifier::TypeSpecWithTemplate: 00913 // These are never namespace scopes. 00914 return true; 00915 } 00916 00917 llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 00918 } 00919 00920 /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 00921 /// scope or nested-name-specifier) is parsed, part of a declarator-id. 00922 /// After this method is called, according to [C++ 3.4.3p3], names should be 00923 /// looked up in the declarator-id's scope, until the declarator is parsed and 00924 /// ActOnCXXExitDeclaratorScope is called. 00925 /// The 'SS' should be a non-empty valid CXXScopeSpec. 00926 bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 00927 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 00928 00929 if (SS.isInvalid()) return true; 00930 00931 DeclContext *DC = computeDeclContext(SS, true); 00932 if (!DC) return true; 00933 00934 // Before we enter a declarator's context, we need to make sure that 00935 // it is a complete declaration context. 00936 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 00937 return true; 00938 00939 EnterDeclaratorContext(S, DC); 00940 00941 // Rebuild the nested name specifier for the new scope. 00942 if (DC->isDependentContext()) 00943 RebuildNestedNameSpecifierInCurrentInstantiation(SS); 00944 00945 return false; 00946 } 00947 00948 /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 00949 /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 00950 /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 00951 /// Used to indicate that names should revert to being looked up in the 00952 /// defining scope. 00953 void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 00954 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 00955 if (SS.isInvalid()) 00956 return; 00957 assert(!SS.isInvalid() && computeDeclContext(SS, true) && 00958 "exiting declarator scope we never really entered"); 00959 ExitDeclaratorContext(S); 00960 }