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 "Sema.h" 00015 #include "Lookup.h" 00016 #include "clang/AST/ASTContext.h" 00017 #include "clang/AST/DeclTemplate.h" 00018 #include "clang/AST/ExprCXX.h" 00019 #include "clang/AST/NestedNameSpecifier.h" 00020 #include "clang/Basic/PartialDiagnostic.h" 00021 #include "clang/Parse/DeclSpec.h" 00022 #include "llvm/ADT/STLExtras.h" 00023 #include "llvm/Support/raw_ostream.h" 00024 using namespace clang; 00025 00026 /// \brief Find the current instantiation that associated with the given type. 00027 static CXXRecordDecl *getCurrentInstantiationOf(QualType T) { 00028 if (T.isNull()) 00029 return 0; 00030 00031 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 00032 if (isa<RecordType>(Ty)) 00033 return cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 00034 else if (isa<InjectedClassNameType>(Ty)) 00035 return cast<InjectedClassNameType>(Ty)->getDecl(); 00036 else 00037 return 0; 00038 } 00039 00040 /// \brief Compute the DeclContext that is associated with the given type. 00041 /// 00042 /// \param T the type for which we are attempting to find a DeclContext. 00043 /// 00044 /// \returns the declaration context represented by the type T, 00045 /// or NULL if the declaration context cannot be computed (e.g., because it is 00046 /// dependent and not the current instantiation). 00047 DeclContext *Sema::computeDeclContext(QualType T) { 00048 if (const TagType *Tag = T->getAs<TagType>()) 00049 return Tag->getDecl(); 00050 00051 return ::getCurrentInstantiationOf(T); 00052 } 00053 00054 /// \brief Compute the DeclContext that is associated with the given 00055 /// scope specifier. 00056 /// 00057 /// \param SS the C++ scope specifier as it appears in the source 00058 /// 00059 /// \param EnteringContext when true, we will be entering the context of 00060 /// this scope specifier, so we can retrieve the declaration context of a 00061 /// class template or class template partial specialization even if it is 00062 /// not the current instantiation. 00063 /// 00064 /// \returns the declaration context represented by the scope specifier @p SS, 00065 /// or NULL if the declaration context cannot be computed (e.g., because it is 00066 /// dependent and not the current instantiation). 00067 DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 00068 bool EnteringContext) { 00069 if (!SS.isSet() || SS.isInvalid()) 00070 return 0; 00071 00072 NestedNameSpecifier *NNS 00073 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00074 if (NNS->isDependent()) { 00075 // If this nested-name-specifier refers to the current 00076 // instantiation, return its DeclContext. 00077 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 00078 return Record; 00079 00080 if (EnteringContext) { 00081 const Type *NNSType = NNS->getAsType(); 00082 if (!NNSType) { 00083 // do nothing, fall out 00084 } else if (const TemplateSpecializationType *SpecType 00085 = NNSType->getAs<TemplateSpecializationType>()) { 00086 // We are entering the context of the nested name specifier, so try to 00087 // match the nested name specifier to either a primary class template 00088 // or a class template partial specialization. 00089 if (ClassTemplateDecl *ClassTemplate 00090 = dyn_cast_or_null<ClassTemplateDecl>( 00091 SpecType->getTemplateName().getAsTemplateDecl())) { 00092 QualType ContextType 00093 = Context.getCanonicalType(QualType(SpecType, 0)); 00094 00095 // If the type of the nested name specifier is the same as the 00096 // injected class name of the named class template, we're entering 00097 // into that class template definition. 00098 QualType Injected 00099 = ClassTemplate->getInjectedClassNameSpecialization(Context); 00100 if (Context.hasSameType(Injected, ContextType)) 00101 return ClassTemplate->getTemplatedDecl(); 00102 00103 // If the type of the nested name specifier is the same as the 00104 // type of one of the class template's class template partial 00105 // specializations, we're entering into the definition of that 00106 // class template partial specialization. 00107 if (ClassTemplatePartialSpecializationDecl *PartialSpec 00108 = ClassTemplate->findPartialSpecialization(ContextType)) 00109 return PartialSpec; 00110 } 00111 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 00112 // The nested name specifier refers to a member of a class template. 00113 return RecordT->getDecl(); 00114 } 00115 } 00116 00117 return 0; 00118 } 00119 00120 switch (NNS->getKind()) { 00121 case NestedNameSpecifier::Identifier: 00122 assert(false && "Dependent nested-name-specifier has no DeclContext"); 00123 break; 00124 00125 case NestedNameSpecifier::Namespace: 00126 return NNS->getAsNamespace(); 00127 00128 case NestedNameSpecifier::TypeSpec: 00129 case NestedNameSpecifier::TypeSpecWithTemplate: { 00130 const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 00131 assert(Tag && "Non-tag type in nested-name-specifier"); 00132 return Tag->getDecl(); 00133 } break; 00134 00135 case NestedNameSpecifier::Global: 00136 return Context.getTranslationUnitDecl(); 00137 } 00138 00139 // Required to silence a GCC warning. 00140 return 0; 00141 } 00142 00143 bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 00144 if (!SS.isSet() || SS.isInvalid()) 00145 return false; 00146 00147 NestedNameSpecifier *NNS 00148 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00149 return NNS->isDependent(); 00150 } 00151 00152 // \brief Determine whether this C++ scope specifier refers to an 00153 // unknown specialization, i.e., a dependent type that is not the 00154 // current instantiation. 00155 bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { 00156 if (!isDependentScopeSpecifier(SS)) 00157 return false; 00158 00159 NestedNameSpecifier *NNS 00160 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00161 return getCurrentInstantiationOf(NNS) == 0; 00162 } 00163 00164 /// \brief If the given nested name specifier refers to the current 00165 /// instantiation, return the declaration that corresponds to that 00166 /// current instantiation (C++0x [temp.dep.type]p1). 00167 /// 00168 /// \param NNS a dependent nested name specifier. 00169 CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 00170 assert(getLangOptions().CPlusPlus && "Only callable in C++"); 00171 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 00172 00173 if (!NNS->getAsType()) 00174 return 0; 00175 00176 QualType T = QualType(NNS->getAsType(), 0); 00177 return ::getCurrentInstantiationOf(T); 00178 } 00179 00180 /// \brief Require that the context specified by SS be complete. 00181 /// 00182 /// If SS refers to a type, this routine checks whether the type is 00183 /// complete enough (or can be made complete enough) for name lookup 00184 /// into the DeclContext. A type that is not yet completed can be 00185 /// considered "complete enough" if it is a class/struct/union/enum 00186 /// that is currently being defined. Or, if we have a type that names 00187 /// a class template specialization that is not a complete type, we 00188 /// will attempt to instantiate that class template. 00189 bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 00190 DeclContext *DC) { 00191 assert(DC != 0 && "given null context"); 00192 00193 if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 00194 // If this is a dependent type, then we consider it complete. 00195 if (Tag->isDependentContext()) 00196 return false; 00197 00198 // If we're currently defining this type, then lookup into the 00199 // type is okay: don't complain that it isn't complete yet. 00200 const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>(); 00201 if (TagT && TagT->isBeingDefined()) 00202 return false; 00203 00204 // The type must be complete. 00205 if (RequireCompleteType(SS.getRange().getBegin(), 00206 Context.getTypeDeclType(Tag), 00207 PDiag(diag::err_incomplete_nested_name_spec) 00208 << SS.getRange())) { 00209 SS.setScopeRep(0); // Mark the ScopeSpec invalid. 00210 return true; 00211 } 00212 } 00213 00214 return false; 00215 } 00216 00217 /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the 00218 /// global scope ('::'). 00219 Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, 00220 SourceLocation CCLoc) { 00221 return NestedNameSpecifier::GlobalSpecifier(Context); 00222 } 00223 00224 /// \brief Determines whether the given declaration is an valid acceptable 00225 /// result for name lookup of a nested-name-specifier. 00226 bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) { 00227 if (!SD) 00228 return false; 00229 00230 // Namespace and namespace aliases are fine. 00231 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) 00232 return true; 00233 00234 if (!isa<TypeDecl>(SD)) 00235 return false; 00236 00237 // Determine whether we have a class (or, in C++0x, an enum) or 00238 // a typedef thereof. If so, build the nested-name-specifier. 00239 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 00240 if (T->isDependentType()) 00241 return true; 00242 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { 00243 if (TD->getUnderlyingType()->isRecordType() || 00244 (Context.getLangOptions().CPlusPlus0x && 00245 TD->getUnderlyingType()->isEnumeralType())) 00246 return true; 00247 } else if (isa<RecordDecl>(SD) || 00248 (Context.getLangOptions().CPlusPlus0x && isa<EnumDecl>(SD))) 00249 return true; 00250 00251 return false; 00252 } 00253 00254 /// \brief If the given nested-name-specifier begins with a bare identifier 00255 /// (e.g., Base::), perform name lookup for that identifier as a 00256 /// nested-name-specifier within the given scope, and return the result of that 00257 /// name lookup. 00258 NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 00259 if (!S || !NNS) 00260 return 0; 00261 00262 while (NNS->getPrefix()) 00263 NNS = NNS->getPrefix(); 00264 00265 if (NNS->getKind() != NestedNameSpecifier::Identifier) 00266 return 0; 00267 00268 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 00269 LookupNestedNameSpecifierName); 00270 LookupName(Found, S); 00271 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 00272 00273 if (!Found.isSingleResult()) 00274 return 0; 00275 00276 NamedDecl *Result = Found.getFoundDecl(); 00277 if (isAcceptableNestedNameSpecifier(Result)) 00278 return Result; 00279 00280 return 0; 00281 } 00282 00283 bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, 00284 SourceLocation IdLoc, 00285 IdentifierInfo &II, 00286 TypeTy *ObjectTypePtr) { 00287 QualType ObjectType = GetTypeFromParser(ObjectTypePtr); 00288 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 00289 00290 // Determine where to perform name lookup 00291 DeclContext *LookupCtx = 0; 00292 bool isDependent = false; 00293 if (!ObjectType.isNull()) { 00294 // This nested-name-specifier occurs in a member access expression, e.g., 00295 // x->B::f, and we are looking into the type of the object. 00296 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 00297 LookupCtx = computeDeclContext(ObjectType); 00298 isDependent = ObjectType->isDependentType(); 00299 } else if (SS.isSet()) { 00300 // This nested-name-specifier occurs after another nested-name-specifier, 00301 // so long into the context associated with the prior nested-name-specifier. 00302 LookupCtx = computeDeclContext(SS, false); 00303 isDependent = isDependentScopeSpecifier(SS); 00304 Found.setContextRange(SS.getRange()); 00305 } 00306 00307 if (LookupCtx) { 00308 // Perform "qualified" name lookup into the declaration context we 00309 // computed, which is either the type of the base of a member access 00310 // expression or the declaration context associated with a prior 00311 // nested-name-specifier. 00312 00313 // The declaration context must be complete. 00314 if (!LookupCtx->isDependentContext() && 00315 RequireCompleteDeclContext(SS, LookupCtx)) 00316 return false; 00317 00318 LookupQualifiedName(Found, LookupCtx); 00319 } else if (isDependent) { 00320 return false; 00321 } else { 00322 LookupName(Found, S); 00323 } 00324 Found.suppressDiagnostics(); 00325 00326 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 00327 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); 00328 00329 return false; 00330 } 00331 00332 /// \brief Build a new nested-name-specifier for "identifier::", as described 00333 /// by ActOnCXXNestedNameSpecifier. 00334 /// 00335 /// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in 00336 /// that it contains an extra parameter \p ScopeLookupResult, which provides 00337 /// the result of name lookup within the scope of the nested-name-specifier 00338 /// that was computed at template definition time. 00339 /// 00340 /// If ErrorRecoveryLookup is true, then this call is used to improve error 00341 /// recovery. This means that it should not emit diagnostics, it should 00342 /// just return null on failure. It also means it should only return a valid 00343 /// scope if it *knows* that the result is correct. It should not return in a 00344 /// dependent context, for example. 00345 Sema::CXXScopeTy *Sema::BuildCXXNestedNameSpecifier(Scope *S, 00346 CXXScopeSpec &SS, 00347 SourceLocation IdLoc, 00348 SourceLocation CCLoc, 00349 IdentifierInfo &II, 00350 QualType ObjectType, 00351 NamedDecl *ScopeLookupResult, 00352 bool EnteringContext, 00353 bool ErrorRecoveryLookup) { 00354 NestedNameSpecifier *Prefix 00355 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00356 00357 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 00358 00359 // Determine where to perform name lookup 00360 DeclContext *LookupCtx = 0; 00361 bool isDependent = false; 00362 if (!ObjectType.isNull()) { 00363 // This nested-name-specifier occurs in a member access expression, e.g., 00364 // x->B::f, and we are looking into the type of the object. 00365 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 00366 LookupCtx = computeDeclContext(ObjectType); 00367 isDependent = ObjectType->isDependentType(); 00368 } else if (SS.isSet()) { 00369 // This nested-name-specifier occurs after another nested-name-specifier, 00370 // so long into the context associated with the prior nested-name-specifier. 00371 LookupCtx = computeDeclContext(SS, EnteringContext); 00372 isDependent = isDependentScopeSpecifier(SS); 00373 Found.setContextRange(SS.getRange()); 00374 } 00375 00376 00377 bool ObjectTypeSearchedInScope = false; 00378 if (LookupCtx) { 00379 // Perform "qualified" name lookup into the declaration context we 00380 // computed, which is either the type of the base of a member access 00381 // expression or the declaration context associated with a prior 00382 // nested-name-specifier. 00383 00384 // The declaration context must be complete. 00385 if (!LookupCtx->isDependentContext() && 00386 RequireCompleteDeclContext(SS, LookupCtx)) 00387 return 0; 00388 00389 LookupQualifiedName(Found, LookupCtx); 00390 00391 if (!ObjectType.isNull() && Found.empty()) { 00392 // C++ [basic.lookup.classref]p4: 00393 // If the id-expression in a class member access is a qualified-id of 00394 // the form 00395 // 00396 // class-name-or-namespace-name::... 00397 // 00398 // the class-name-or-namespace-name following the . or -> operator is 00399 // looked up both in the context of the entire postfix-expression and in 00400 // the scope of the class of the object expression. If the name is found 00401 // only in the scope of the class of the object expression, the name 00402 // shall refer to a class-name. If the name is found only in the 00403 // context of the entire postfix-expression, the name shall refer to a 00404 // class-name or namespace-name. [...] 00405 // 00406 // Qualified name lookup into a class will not find a namespace-name, 00407 // so we do not need to diagnoste that case specifically. However, 00408 // this qualified name lookup may find nothing. In that case, perform 00409 // unqualified name lookup in the given scope (if available) or 00410 // reconstruct the result from when name lookup was performed at template 00411 // definition time. 00412 if (S) 00413 LookupName(Found, S); 00414 else if (ScopeLookupResult) 00415 Found.addDecl(ScopeLookupResult); 00416 00417 ObjectTypeSearchedInScope = true; 00418 } 00419 } else if (isDependent) { 00420 // Don't speculate if we're just trying to improve error recovery. 00421 if (ErrorRecoveryLookup) 00422 return 0; 00423 00424 // We were not able to compute the declaration context for a dependent 00425 // base object type or prior nested-name-specifier, so this 00426 // nested-name-specifier refers to an unknown specialization. Just build 00427 // a dependent nested-name-specifier. 00428 if (!Prefix) 00429 return NestedNameSpecifier::Create(Context, &II); 00430 00431 return NestedNameSpecifier::Create(Context, Prefix, &II); 00432 } else { 00433 // Perform unqualified name lookup in the current scope. 00434 LookupName(Found, S); 00435 } 00436 00437 // FIXME: Deal with ambiguities cleanly. 00438 00439 if (Found.empty() && !ErrorRecoveryLookup) { 00440 // We haven't found anything, and we're not recovering from a 00441 // different kind of error, so look for typos. 00442 DeclarationName Name = Found.getLookupName(); 00443 if (CorrectTypo(Found, S, &SS, LookupCtx, EnteringContext, 00444 CTC_NoKeywords) && 00445 Found.isSingleResult() && 00446 isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>())) { 00447 if (LookupCtx) 00448 Diag(Found.getNameLoc(), diag::err_no_member_suggest) 00449 << Name << LookupCtx << Found.getLookupName() << SS.getRange() 00450 << FixItHint::CreateReplacement(Found.getNameLoc(), 00451 Found.getLookupName().getAsString()); 00452 else 00453 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest) 00454 << Name << Found.getLookupName() 00455 << FixItHint::CreateReplacement(Found.getNameLoc(), 00456 Found.getLookupName().getAsString()); 00457 00458 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 00459 Diag(ND->getLocation(), diag::note_previous_decl) 00460 << ND->getDeclName(); 00461 } else 00462 Found.clear(); 00463 } 00464 00465 NamedDecl *SD = Found.getAsSingle<NamedDecl>(); 00466 if (isAcceptableNestedNameSpecifier(SD)) { 00467 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) { 00468 // C++ [basic.lookup.classref]p4: 00469 // [...] If the name is found in both contexts, the 00470 // class-name-or-namespace-name shall refer to the same entity. 00471 // 00472 // We already found the name in the scope of the object. Now, look 00473 // into the current scope (the scope of the postfix-expression) to 00474 // see if we can find the same name there. As above, if there is no 00475 // scope, reconstruct the result from the template instantiation itself. 00476 NamedDecl *OuterDecl; 00477 if (S) { 00478 LookupResult FoundOuter(*this, &II, IdLoc, LookupNestedNameSpecifierName); 00479 LookupName(FoundOuter, S); 00480 OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 00481 } else 00482 OuterDecl = ScopeLookupResult; 00483 00484 if (isAcceptableNestedNameSpecifier(OuterDecl) && 00485 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 00486 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 00487 !Context.hasSameType( 00488 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 00489 Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 00490 if (ErrorRecoveryLookup) 00491 return 0; 00492 00493 Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous) 00494 << &II; 00495 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 00496 << ObjectType; 00497 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 00498 00499 // Fall through so that we'll pick the name we found in the object 00500 // type, since that's probably what the user wanted anyway. 00501 } 00502 } 00503 00504 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) 00505 return NestedNameSpecifier::Create(Context, Prefix, Namespace); 00506 00507 // FIXME: It would be nice to maintain the namespace alias name, then 00508 // see through that alias when resolving the nested-name-specifier down to 00509 // a declaration context. 00510 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) 00511 return NestedNameSpecifier::Create(Context, Prefix, 00512 00513 Alias->getNamespace()); 00514 00515 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 00516 return NestedNameSpecifier::Create(Context, Prefix, false, 00517 T.getTypePtr()); 00518 } 00519 00520 // Otherwise, we have an error case. If we don't want diagnostics, just 00521 // return an error now. 00522 if (ErrorRecoveryLookup) 00523 return 0; 00524 00525 // If we didn't find anything during our lookup, try again with 00526 // ordinary name lookup, which can help us produce better error 00527 // messages. 00528 if (Found.empty()) { 00529 Found.clear(LookupOrdinaryName); 00530 LookupName(Found, S); 00531 } 00532 00533 unsigned DiagID; 00534 if (!Found.empty()) 00535 DiagID = diag::err_expected_class_or_namespace; 00536 else if (SS.isSet()) { 00537 Diag(IdLoc, diag::err_no_member) << &II << LookupCtx << SS.getRange(); 00538 return 0; 00539 } else 00540 DiagID = diag::err_undeclared_var_use; 00541 00542 if (SS.isSet()) 00543 Diag(IdLoc, DiagID) << &II << SS.getRange(); 00544 else 00545 Diag(IdLoc, DiagID) << &II; 00546 00547 return 0; 00548 } 00549 00550 /// ActOnCXXNestedNameSpecifier - Called during parsing of a 00551 /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now 00552 /// we want to resolve "bar::". 'SS' is empty or the previously parsed 00553 /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', 00554 /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. 00555 /// Returns a CXXScopeTy* object representing the C++ scope. 00556 Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 00557 CXXScopeSpec &SS, 00558 SourceLocation IdLoc, 00559 SourceLocation CCLoc, 00560 IdentifierInfo &II, 00561 TypeTy *ObjectTypePtr, 00562 bool EnteringContext) { 00563 return BuildCXXNestedNameSpecifier(S, SS, IdLoc, CCLoc, II, 00564 QualType::getFromOpaquePtr(ObjectTypePtr), 00565 /*ScopeLookupResult=*/0, EnteringContext, 00566 false); 00567 } 00568 00569 /// IsInvalidUnlessNestedName - This method is used for error recovery 00570 /// purposes to determine whether the specified identifier is only valid as 00571 /// a nested name specifier, for example a namespace name. It is 00572 /// conservatively correct to always return false from this method. 00573 /// 00574 /// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. 00575 bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 00576 IdentifierInfo &II, TypeTy *ObjectType, 00577 bool EnteringContext) { 00578 return BuildCXXNestedNameSpecifier(S, SS, SourceLocation(), SourceLocation(), 00579 II, QualType::getFromOpaquePtr(ObjectType), 00580 /*ScopeLookupResult=*/0, EnteringContext, 00581 true); 00582 } 00583 00584 Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 00585 const CXXScopeSpec &SS, 00586 TypeTy *Ty, 00587 SourceRange TypeRange, 00588 SourceLocation CCLoc) { 00589 NestedNameSpecifier *Prefix 00590 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 00591 QualType T = GetTypeFromParser(Ty); 00592 return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false, 00593 T.getTypePtr()); 00594 } 00595 00596 bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 00597 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 00598 00599 NestedNameSpecifier *Qualifier = 00600 static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 00601 00602 // There are only two places a well-formed program may qualify a 00603 // declarator: first, when defining a namespace or class member 00604 // out-of-line, and second, when naming an explicitly-qualified 00605 // friend function. The latter case is governed by 00606 // C++03 [basic.lookup.unqual]p10: 00607 // In a friend declaration naming a member function, a name used 00608 // in the function declarator and not part of a template-argument 00609 // in a template-id is first looked up in the scope of the member 00610 // function's class. If it is not found, or if the name is part of 00611 // a template-argument in a template-id, the look up is as 00612 // described for unqualified names in the definition of the class 00613 // granting friendship. 00614 // i.e. we don't push a scope unless it's a class member. 00615 00616 switch (Qualifier->getKind()) { 00617 case NestedNameSpecifier::Global: 00618 case NestedNameSpecifier::Namespace: 00619 // These are always namespace scopes. We never want to enter a 00620 // namespace scope from anything but a file context. 00621 return CurContext->getLookupContext()->isFileContext(); 00622 00623 case NestedNameSpecifier::Identifier: 00624 case NestedNameSpecifier::TypeSpec: 00625 case NestedNameSpecifier::TypeSpecWithTemplate: 00626 // These are never namespace scopes. 00627 return true; 00628 } 00629 00630 // Silence bogus warning. 00631 return false; 00632 } 00633 00634 /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 00635 /// scope or nested-name-specifier) is parsed, part of a declarator-id. 00636 /// After this method is called, according to [C++ 3.4.3p3], names should be 00637 /// looked up in the declarator-id's scope, until the declarator is parsed and 00638 /// ActOnCXXExitDeclaratorScope is called. 00639 /// The 'SS' should be a non-empty valid CXXScopeSpec. 00640 bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 00641 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 00642 00643 if (SS.isInvalid()) return true; 00644 00645 DeclContext *DC = computeDeclContext(SS, true); 00646 if (!DC) return true; 00647 00648 // Before we enter a declarator's context, we need to make sure that 00649 // it is a complete declaration context. 00650 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 00651 return true; 00652 00653 EnterDeclaratorContext(S, DC); 00654 00655 // Rebuild the nested name specifier for the new scope. 00656 if (DC->isDependentContext()) 00657 RebuildNestedNameSpecifierInCurrentInstantiation(SS); 00658 00659 return false; 00660 } 00661 00662 /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 00663 /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 00664 /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 00665 /// Used to indicate that names should revert to being looked up in the 00666 /// defining scope. 00667 void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 00668 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 00669 if (SS.isInvalid()) 00670 return; 00671 assert(!SS.isInvalid() && computeDeclContext(SS, true) && 00672 "exiting declarator scope we never really entered"); 00673 ExitDeclaratorContext(S); 00674 }