SemaCXXScopeSpec.cpp revision 20c6b3b85e186cd52d5d99489132d71d498159eb
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 "Sema.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/Parse/DeclSpec.h" 17#include "clang/Basic/DiagnosticSema.h" 18#include "llvm/ADT/STLExtras.h" 19using namespace clang; 20 21 22/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the 23/// global scope ('::'). 24Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, 25 SourceLocation CCLoc) { 26 return cast<DeclContext>(Context.getTranslationUnitDecl()); 27} 28 29/// ActOnCXXNestedNameSpecifier - Called during parsing of a 30/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now 31/// we want to resolve "bar::". 'SS' is empty or the previously parsed 32/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', 33/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. 34/// Returns a CXXScopeTy* object representing the C++ scope. 35Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 36 const CXXScopeSpec &SS, 37 SourceLocation IdLoc, 38 SourceLocation CCLoc, 39 IdentifierInfo &II) { 40 Decl *SD = LookupParsedName(S, SS, &II, 41 LookupCriteria(LookupCriteria::NestedNameSpecifier, 42 /*RedeclarationOnly=*/false, 43 /*CPlusPlus=*/true)); 44 45 if (SD) { 46 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { 47 if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType()) 48 return cast<DeclContext>(Record->getDecl()); 49 } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) { 50 return cast<DeclContext>(SD); 51 } 52 53 // FIXME: Template parameters and dependent types. 54 // FIXME: C++0x scoped enums 55 56 // Fall through to produce an error: we found something that isn't 57 // a class or a namespace. 58 } 59 60 // If we didn't find anything during our lookup, try again with 61 // ordinary name lookup, which can help us produce better error 62 // messages. 63 if (!SD) 64 SD = LookupParsedName(S, SS, &II, 65 LookupCriteria(LookupCriteria::Ordinary, 66 /*RedeclarationOnly=*/false, 67 /*CPlusPlus=*/true)); 68 unsigned DiagID; 69 if (SD) 70 DiagID = diag::err_expected_class_or_namespace; 71 else if (SS.isSet()) 72 DiagID = diag::err_typecheck_no_member; 73 else 74 DiagID = diag::err_undeclared_var_use; 75 76 if (SS.isSet()) 77 Diag(IdLoc, DiagID) << &II << SS.getRange(); 78 else 79 Diag(IdLoc, DiagID) << &II; 80 81 return 0; 82} 83 84/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 85/// scope or nested-name-specifier) is parsed, part of a declarator-id. 86/// After this method is called, according to [C++ 3.4.3p3], names should be 87/// looked up in the declarator-id's scope, until the declarator is parsed and 88/// ActOnCXXExitDeclaratorScope is called. 89/// The 'SS' should be a non-empty valid CXXScopeSpec. 90void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 91 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 92 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?"); 93 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity()); 94 CurContext = static_cast<DeclContext*>(SS.getScopeRep()); 95 S->setEntity(CurContext); 96} 97 98/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 99/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 100/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 101/// Used to indicate that names should revert to being looked up in the 102/// defining scope. 103void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 104 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 105 assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!"); 106 S->setEntity(PreDeclaratorDC); 107 PreDeclaratorDC = 0; 108 109 // Reset CurContext to the nearest enclosing context. 110 while (!S->getEntity() && S->getParent()) 111 S = S->getParent(); 112 CurContext = static_cast<DeclContext*>(S->getEntity()); 113} 114