The scope representation can now be either a DeclContext pointer or a
Type pointer. This allows our nested-name-specifiers to retain more
information about the actual spelling (e.g., which typedef did the
user name, or what exact template arguments were used in the
template-id?). It will also allow us to have dependent
nested-name-specifiers that don't map to any DeclContext.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67140 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaCXXScopeSpec.cpp b/lib/Sema/SemaCXXScopeSpec.cpp
index 7773d28..e30ec2a 100644
--- a/lib/Sema/SemaCXXScopeSpec.cpp
+++ b/lib/Sema/SemaCXXScopeSpec.cpp
@@ -17,6 +17,49 @@
 #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
@@ -30,7 +73,7 @@
   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.
@@ -52,7 +95,7 @@
 /// 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
@@ -70,10 +113,10 @@
 
   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.
@@ -109,10 +152,7 @@
                                                     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
@@ -125,7 +165,7 @@
   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);
 }
 
@@ -136,7 +176,8 @@
 /// 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;