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