SemaCXXScopeSpec.cpp revision 193326
1105197Ssam//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 2105197Ssam// 3105197Ssam// The LLVM Compiler Infrastructure 4139823Simp// 5105197Ssam// This file is distributed under the University of Illinois Open Source 6105197Ssam// License. See LICENSE.TXT for details. 7105197Ssam// 8105197Ssam//===----------------------------------------------------------------------===// 9105197Ssam// 10105197Ssam// This file implements C++ semantic analysis for scope specifiers. 11105197Ssam// 12105197Ssam//===----------------------------------------------------------------------===// 13105197Ssam 14105197Ssam#include "Sema.h" 15105197Ssam#include "clang/AST/ASTContext.h" 16105197Ssam#include "clang/AST/DeclTemplate.h" 17105197Ssam#include "clang/AST/NestedNameSpecifier.h" 18105197Ssam#include "clang/Parse/DeclSpec.h" 19105197Ssam#include "llvm/ADT/STLExtras.h" 20105197Ssamusing namespace clang; 21105197Ssam 22105197Ssam/// \brief Compute the DeclContext that is associated with the given 23105197Ssam/// scope specifier. 24105197SsamDeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS) { 25105197Ssam if (!SS.isSet() || SS.isInvalid()) 26105197Ssam return 0; 27105197Ssam 28105197Ssam NestedNameSpecifier *NNS 29105197Ssam = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 30105197Ssam if (NNS->isDependent()) { 31105197Ssam // If this nested-name-specifier refers to the current 32105197Ssam // instantiation, return its DeclContext. 33105197Ssam if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 34105197Ssam return Record; 35105197Ssam else 36105197Ssam return 0; 37105197Ssam } 38105197Ssam 39105197Ssam switch (NNS->getKind()) { 40105197Ssam case NestedNameSpecifier::Identifier: 41105197Ssam assert(false && "Dependent nested-name-specifier has no DeclContext"); 42105197Ssam break; 43105197Ssam 44105197Ssam case NestedNameSpecifier::Namespace: 45119643Ssam return NNS->getAsNamespace(); 46119643Ssam 47105197Ssam case NestedNameSpecifier::TypeSpec: 48105197Ssam case NestedNameSpecifier::TypeSpecWithTemplate: { 49105197Ssam const TagType *Tag = NNS->getAsType()->getAsTagType(); 50105197Ssam assert(Tag && "Non-tag type in nested-name-specifier"); 51105197Ssam return Tag->getDecl(); 52105197Ssam } break; 53105197Ssam 54105197Ssam case NestedNameSpecifier::Global: 55105197Ssam return Context.getTranslationUnitDecl(); 56105197Ssam } 57158767Spjd 58105197Ssam // Required to silence a GCC warning. 59183550Szec return 0; 60105197Ssam} 61105197Ssam 62105197Ssambool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 63105197Ssam if (!SS.isSet() || SS.isInvalid()) 64105197Ssam return false; 65105197Ssam 66105197Ssam NestedNameSpecifier *NNS 67105197Ssam = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 68105197Ssam return NNS->isDependent(); 69105197Ssam} 70105197Ssam 71105197Ssam// \brief Determine whether this C++ scope specifier refers to an 72105197Ssam// unknown specialization, i.e., a dependent type that is not the 73105197Ssam// current instantiation. 74105197Ssambool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { 75105197Ssam if (!isDependentScopeSpecifier(SS)) 76105197Ssam return false; 77105197Ssam 78185571Sbz NestedNameSpecifier *NNS 79105197Ssam = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 80105197Ssam return getCurrentInstantiationOf(NNS) == 0; 81105197Ssam} 82185571Sbz 83105197Ssam/// \brief If the given nested name specifier refers to the current 84105197Ssam/// instantiation, return the declaration that corresponds to that 85105197Ssam/// current instantiation (C++0x [temp.dep.type]p1). 86105197Ssam/// 87105197Ssam/// \param NNS a dependent nested name specifier. 88105197SsamCXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 89105197Ssam assert(getLangOptions().CPlusPlus && "Only callable in C++"); 90105197Ssam assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 91105197Ssam 92105197Ssam QualType T = QualType(NNS->getAsType(), 0); 93105197Ssam // If the nested name specifier does not refer to a type, then it 94105197Ssam // does not refer to the current instantiation. 95105197Ssam if (T.isNull()) 96105197Ssam return 0; 97105197Ssam 98105197Ssam T = Context.getCanonicalType(T); 99105197Ssam 100105197Ssam for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getParent()) { 101105197Ssam // If we've hit a namespace or the global scope, then the 102181803Sbz // nested-name-specifier can't refer to the current instantiation. 103105197Ssam if (Ctx->isFileContext()) 104105197Ssam return 0; 105105197Ssam 106105197Ssam // Skip non-class contexts. 107105197Ssam CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); 108105197Ssam if (!Record) 109105197Ssam continue; 110105197Ssam 111105197Ssam // If this record type is not dependent, 112105197Ssam if (!Record->isDependentType()) 113105197Ssam return 0; 114105197Ssam 115105197Ssam // C++ [temp.dep.type]p1: 116105197Ssam // 117105197Ssam // In the definition of a class template, a nested class of a 118185088Szec // class template, a member of a class template, or a member of a 119185088Szec // nested class of a class template, a name refers to the current 120185088Szec // instantiation if it is 121185088Szec // -- the injected-class-name (9) of the class template or 122185088Szec // nested class, 123185088Szec // -- in the definition of a primary class template, the name 124185088Szec // of the class template followed by the template argument 125185088Szec // list of the primary template (as described below) 126185088Szec // enclosed in <>, 127185088Szec // -- in the definition of a nested class of a class template, 128185088Szec // the name of the nested class referenced as a member of 129105197Ssam // the current instantiation, or 130185088Szec // -- in the definition of a partial specialization, the name 131105197Ssam // of the class template followed by the template argument 132185088Szec // list of the partial specialization enclosed in <>. If 133185088Szec // the nth template parameter is a parameter pack, the nth 134185088Szec // template argument is a pack expansion (14.6.3) whose 135185088Szec // pattern is the name of the parameter pack. (FIXME) 136105197Ssam // 137185088Szec // All of these options come down to having the 138185088Szec // nested-name-specifier type that is equivalent to the 139185088Szec // injected-class-name of one of the types that is currently in 140185088Szec // our context. 141185088Szec if (Context.getTypeDeclType(Record) == T) 142185088Szec return Record; 143119643Ssam 144120585Ssam if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { 145120585Ssam QualType InjectedClassName 146120585Ssam = Template->getInjectedClassNameType(Context); 147120585Ssam if (T == Context.getCanonicalType(InjectedClassName)) 148120585Ssam return Template->getTemplatedDecl(); 149120585Ssam } 150120585Ssam } 151120585Ssam 152119643Ssam return 0; 153120585Ssam} 154120585Ssam 155120585Ssam/// \brief Require that the context specified by SS be complete. 156120585Ssam/// 157120585Ssam/// If SS refers to a type, this routine checks whether the type is 158120585Ssam/// complete enough (or can be made complete enough) for name lookup 159120585Ssam/// into the DeclContext. A type that is not yet completed can be 160120585Ssam/// considered "complete enough" if it is a class/struct/union/enum 161119643Ssam/// that is currently being defined. Or, if we have a type that names 162119643Ssam/// a class template specialization that is not a complete type, we 163120585Ssam/// will attempt to instantiate that class template. 164120585Ssambool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) { 165120585Ssam if (!SS.isSet() || SS.isInvalid()) 166120585Ssam return false; 167120585Ssam 168120585Ssam DeclContext *DC = computeDeclContext(SS); 169120585Ssam if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 170119643Ssam // If we're currently defining this type, then lookup into the 171120585Ssam // type is okay: don't complain that it isn't complete yet. 172120585Ssam const TagType *TagT = Context.getTypeDeclType(Tag)->getAsTagType(); 173120585Ssam if (TagT->isBeingDefined()) 174120585Ssam return false; 175120585Ssam 176120585Ssam // The type must be complete. 177120585Ssam return RequireCompleteType(SS.getRange().getBegin(), 178119643Ssam Context.getTypeDeclType(Tag), 179120585Ssam diag::err_incomplete_nested_name_spec, 180120585Ssam SS.getRange()); 181120585Ssam } 182120585Ssam 183120585Ssam return false; 184120585Ssam} 185120585Ssam 186105197Ssam/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the 187105197Ssam/// global scope ('::'). 188128856SsamSema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, 189105197Ssam SourceLocation CCLoc) { 190105197Ssam return NestedNameSpecifier::GlobalSpecifier(Context); 191128856Ssam} 192128856Ssam 193128856Ssam/// ActOnCXXNestedNameSpecifier - Called during parsing of a 194185348Szec/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now 195105197Ssam/// we want to resolve "bar::". 'SS' is empty or the previously parsed 196105197Ssam/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', 197105197Ssam/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. 198185348Szec/// Returns a CXXScopeTy* object representing the C++ scope. 199105197SsamSema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 200105197Ssam const CXXScopeSpec &SS, 201105197Ssam SourceLocation IdLoc, 202105197Ssam SourceLocation CCLoc, 203105197Ssam IdentifierInfo &II) { 204105197Ssam NestedNameSpecifier *Prefix 205105197Ssam = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 206105197Ssam 207105197Ssam // If the prefix already refers to an unknown specialization, there 208105197Ssam // is no name lookup to perform. Just build the resulting 209105197Ssam // nested-name-specifier. 210105197Ssam if (Prefix && isUnknownSpecialization(SS)) 211105197Ssam return NestedNameSpecifier::Create(Context, Prefix, &II); 212105197Ssam 213105197Ssam NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName); 214105197Ssam 215105197Ssam if (SD) { 216105197Ssam if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) 217105197Ssam return NestedNameSpecifier::Create(Context, Prefix, Namespace); 218105197Ssam 219105197Ssam if (TypeDecl *Type = dyn_cast<TypeDecl>(SD)) { 220105197Ssam // Determine whether we have a class (or, in C++0x, an enum) or 221105197Ssam // a typedef thereof. If so, build the nested-name-specifier. 222105197Ssam QualType T = Context.getTypeDeclType(Type); 223105197Ssam bool AcceptableType = false; 224105197Ssam if (T->isDependentType()) 225105197Ssam AcceptableType = true; 226105197Ssam else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { 227105197Ssam if (TD->getUnderlyingType()->isRecordType() || 228105197Ssam (getLangOptions().CPlusPlus0x && 229105197Ssam TD->getUnderlyingType()->isEnumeralType())) 230105197Ssam AcceptableType = true; 231105197Ssam } else if (isa<RecordDecl>(Type) || 232105197Ssam (getLangOptions().CPlusPlus0x && isa<EnumDecl>(Type))) 233105197Ssam AcceptableType = true; 234105197Ssam 235105197Ssam if (AcceptableType) 236105197Ssam return NestedNameSpecifier::Create(Context, Prefix, false, 237105197Ssam T.getTypePtr()); 238105197Ssam } 239105197Ssam 240105197Ssam if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) 241105197Ssam return NestedNameSpecifier::Create(Context, Prefix, 242105197Ssam Alias->getNamespace()); 243105197Ssam 244105197Ssam // Fall through to produce an error: we found something that isn't 245105197Ssam // a class or a namespace. 246105197Ssam } 247105197Ssam 248105197Ssam // If we didn't find anything during our lookup, try again with 249105197Ssam // ordinary name lookup, which can help us produce better error 250105197Ssam // messages. 251105197Ssam if (!SD) 252183550Szec SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName); 253183550Szec unsigned DiagID; 254105197Ssam if (SD) 255105197Ssam DiagID = diag::err_expected_class_or_namespace; 256183550Szec else if (SS.isSet()) 257183550Szec DiagID = diag::err_typecheck_no_member; 258105197Ssam else 259105197Ssam DiagID = diag::err_undeclared_var_use; 260183550Szec 261183550Szec if (SS.isSet()) 262105197Ssam Diag(IdLoc, DiagID) << &II << SS.getRange(); 263105197Ssam else 264183550Szec Diag(IdLoc, DiagID) << &II; 265183550Szec 266105197Ssam return 0; 267105197Ssam} 268183550Szec 269183550SzecSema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 270105197Ssam const CXXScopeSpec &SS, 271105197Ssam TypeTy *Ty, 272183550Szec SourceRange TypeRange, 273183550Szec SourceLocation CCLoc) { 274105197Ssam NestedNameSpecifier *Prefix 275105197Ssam = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 276183550Szec QualType T = QualType::getFromOpaquePtr(Ty); 277183550Szec return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false, 278105197Ssam T.getTypePtr()); 279105197Ssam} 280183550Szec 281183550Szec/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 282105197Ssam/// scope or nested-name-specifier) is parsed, part of a declarator-id. 283105197Ssam/// After this method is called, according to [C++ 3.4.3p3], names should be 284183550Szec/// looked up in the declarator-id's scope, until the declarator is parsed and 285183550Szec/// ActOnCXXExitDeclaratorScope is called. 286105197Ssam/// The 'SS' should be a non-empty valid CXXScopeSpec. 287105197Ssamvoid Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 288183550Szec assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 289183550Szec assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?"); 290105197Ssam PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity()); 291105197Ssam CurContext = computeDeclContext(SS); 292183550Szec assert(CurContext && "No context?"); 293183550Szec S->setEntity(CurContext); 294105197Ssam} 295105197Ssam 296183550Szec/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 297183550Szec/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 298105197Ssam/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 299105197Ssam/// Used to indicate that names should revert to being looked up in the 300105197Ssam/// defining scope. 301105197Ssamvoid Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 302105197Ssam assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 303105197Ssam assert(S->getEntity() == computeDeclContext(SS) && "Context imbalance!"); 304105197Ssam S->setEntity(PreDeclaratorDC); 305105197Ssam PreDeclaratorDC = 0; 306105197Ssam 307105197Ssam // Reset CurContext to the nearest enclosing context. 308105197Ssam while (!S->getEntity() && S->getParent()) 309105197Ssam S = S->getParent(); 310105197Ssam CurContext = static_cast<DeclContext*>(S->getEntity()); 311105197Ssam assert(CurContext && "No context?"); 312105197Ssam} 313105197Ssam