bool RequireCompleteDeclContext(const CXXScopeSpec &SS);
+ /// \brief Build a scope representation from a declaration context.
+ CXXScopeTy *createScopeRep(DeclContext *DC) {
+ return static_cast<CXXScopeTy *>(DC);
+ }
+
+ /// \brief Build a scope representation from a type.
+ CXXScopeTy *createScopeRep(QualType T);
+
+ DeclContext *getScopeRepAsDeclContext(const CXXScopeSpec &SS);
+ QualType getScopeRepAsType(const CXXScopeSpec &SS);
+
+ /// \brief Determines whether this scope specifier is represented as
+ /// a type.
+ bool isScopeRepType(const CXXScopeSpec &SS);
+
/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
/// global scope ('::').
virtual CXXScopeTy *ActOnCXXGlobalScopeSpecifier(Scope *S,
#include "llvm/ADT/STLExtras.h"
using namespace clang;
+/// \brief Retrieve the scope represented by this scope specifier as a
+/// DeclContext.
+DeclContext *Sema::getScopeRepAsDeclContext(const CXXScopeSpec &SS) {
+ if (SS.isInvalid() || !SS.getScopeRep())
+ return 0;
+ uintptr_t Rep = reinterpret_cast<uintptr_t>(SS.getScopeRep());
+ if ((Rep & 0x01) == 0)
+ return reinterpret_cast<DeclContext *>(Rep);
+
+ // Retrieve the DeclContext associated with this type.
+ QualType T = QualType(reinterpret_cast<Type *>(Rep & ~0x01), 0);
+ const TagType *TagT = T->getAsTagType();
+ assert(TagT && "No DeclContext from a non-tag type");
+ return TagT->getDecl();
+}
+
+/// \brief Retrieve the scope represented by this scope specifier as a
+/// type.
+QualType Sema::getScopeRepAsType(const CXXScopeSpec &SS) {
+ if (SS.isInvalid() || !SS.getScopeRep())
+ return QualType();
+
+ uintptr_t Rep = reinterpret_cast<uintptr_t>(SS.getScopeRep());
+ if ((Rep & 0x01) == 0)
+ return QualType();
+ return QualType(reinterpret_cast<Type *>(Rep & ~0x01), 0);
+}
+
+Action::CXXScopeTy *Sema::createScopeRep(QualType T) {
+ assert(((reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()) & 0x01) == 0) &&
+ "Scope type with cv-qualifiers");
+ if (T.isNull())
+ return 0;
+
+ return reinterpret_cast<CXXScopeTy *>(
+ reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()) | 0x01);
+}
+
+bool Sema::isScopeRepType(const CXXScopeSpec &SS) {
+ uintptr_t Rep = reinterpret_cast<uintptr_t>(SS.getScopeRep());
+ return Rep & 0x01;
+}
+
/// \brief Require that the context specified by SS be complete.
///
/// If SS refers to a type, this routine checks whether the type is
if (!SS.isSet() || SS.isInvalid())
return false;
- DeclContext *DC = static_cast<DeclContext *>(SS.getScopeRep());
+ DeclContext *DC = getScopeRepAsDeclContext(SS);
if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
// If we're currently defining this type, then lookup into the
// type is okay: don't complain that it isn't complete yet.
/// global scope ('::').
Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
SourceLocation CCLoc) {
- return cast<DeclContext>(Context.getTranslationUnitDecl());
+ return createScopeRep(Context.getTranslationUnitDecl());
}
/// ActOnCXXNestedNameSpecifier - Called during parsing of a
if (SD) {
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
- if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
- return cast<DeclContext>(Record->getDecl());
+ if (TD->getUnderlyingType()->isRecordType())
+ return createScopeRep(Context.getTypeDeclType(TD));
} else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
- return cast<DeclContext>(SD);
+ return createScopeRep(cast<DeclContext>(SD));
}
// FIXME: Template parameters and dependent types.
TypeTy *Ty,
SourceRange TypeRange,
SourceLocation CCLoc) {
- QualType Type = QualType::getFromOpaquePtr(Ty);
- assert(Type->isRecordType() &&
- "Types in a nested-name-specifier always refer to a record type");
- return cast<DeclContext>(Type->getAsRecordType()->getDecl());
+ return createScopeRep(QualType::getFromOpaquePtr(Ty));
}
/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
- CurContext = static_cast<DeclContext*>(SS.getScopeRep());
+ CurContext = getScopeRepAsDeclContext(SS);
S->setEntity(CurContext);
}
/// defining scope.
void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
- assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
+ assert(S->getEntity() == getScopeRepAsDeclContext(SS) &&
+ "Context imbalance!");
S->setEntity(PreDeclaratorDC);
PreDeclaratorDC = 0;
// implicit member ref, because we want a pointer to the member in general,
// not any specific instance's member.
if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) {
- DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep());
+ DeclContext *DC = getScopeRepAsDeclContext(*SS);
if (D && isa<CXXRecordDecl>(DC)) {
QualType DType;
if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
// - a nested-name-specifier that contains a class-name that
// names a dependent type.
else if (SS && !SS->isEmpty()) {
- for (DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep());
+ for (DeclContext *DC = getScopeRepAsDeclContext(*SS);
DC; DC = DC->getParent()) {
// FIXME: could stop early at namespace scope.
if (DC->isRecord()) {