Desugaring optimizations.  Add single-step desugaring methods to all
concrete types.  Use unqualified desugaring for getAs<> and sundry.
Fix a few users to either not desugar or use qualified desugar, as seemed
appropriate.  Removed Type's qualified desugar method, as it was easy
to accidentally use instead of QualType's.




git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83116 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index 70b9fa5..0293862 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -22,12 +22,12 @@
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
-bool QualType::isConstant(ASTContext &Ctx) const {
-  if (isConstQualified())
+bool QualType::isConstant(QualType T, ASTContext &Ctx) {
+  if (T.isConstQualified())
     return true;
 
-  if (getTypePtr()->isArrayType())
-    return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
+  if (const ArrayType *AT = Ctx.getAsArrayType(T))
+    return AT->getElementType().isConstant(Ctx);
 
   return false;
 }
@@ -106,7 +106,8 @@
 
   // If this is a typedef for an array type, strip the typedef off without
   // losing all typedef information.
-  return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
+  return cast<ArrayType>(getUnqualifiedDesugaredType())
+    ->getElementType().getTypePtr();
 }
 
 /// getDesugaredType - Return the specified type with any "sugar" removed from
@@ -115,62 +116,46 @@
 /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
 /// concrete.
-///
-/// \param ForDisplay When true, the desugaring is provided for
-/// display purposes only. In this case, we apply more heuristics to
-/// decide whether it is worth providing a desugared form of the type
-/// or not.
-QualType QualType::getDesugaredType(bool ForDisplay) const {
+QualType QualType::getDesugaredType(QualType T) {
   QualifierCollector Qs;
-  return Qs.apply(Qs.strip(*this)->getDesugaredType(ForDisplay));
+
+  QualType Cur = T;
+  while (true) {
+    const Type *CurTy = Qs.strip(Cur);
+    switch (CurTy->getTypeClass()) {
+#define ABSTRACT_TYPE(Class, Parent)
+#define TYPE(Class, Parent) \
+    case Type::Class: { \
+      const Class##Type *Ty = cast<Class##Type>(CurTy); \
+      if (!Ty->isSugared()) \
+        return Qs.apply(Cur); \
+      Cur = Ty->desugar(); \
+      break; \
+    }
+#include "clang/AST/TypeNodes.def"
+    }
+  }
 }
 
-/// getDesugaredType - Return the specified type with any "sugar" removed from
-/// type type.  This takes off typedefs, typeof's etc.  If the outer level of
-/// the type is already concrete, it returns it unmodified.  This is similar
-/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
-/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
-/// concrete.
-///
-/// \param ForDisplay When true, the desugaring is provided for
-/// display purposes only. In this case, we apply more heuristics to
-/// decide whether it is worth providing a desugared form of the type
-/// or not.
-QualType Type::getDesugaredType(bool ForDisplay) const {
-  if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
-    return TDT->LookThroughTypedefs().getDesugaredType();
-  if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(this))
-    return ET->getUnderlyingType().getDesugaredType();
-  if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
-    return TOE->getUnderlyingExpr()->getType().getDesugaredType();
-  if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
-    return TOT->getUnderlyingType().getDesugaredType();
-  if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
-    if (!DTT->getUnderlyingType()->isDependentType())
-      return DTT->getUnderlyingType().getDesugaredType();
-  }
-  if (const TemplateSpecializationType *Spec
-        = dyn_cast<TemplateSpecializationType>(this)) {
-    if (ForDisplay)
-      return QualType(this, 0);
+/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
+/// sugar off the given type.  This should produce an object of the
+/// same dynamic type as the canonical type.
+const Type *Type::getUnqualifiedDesugaredType() const {
+  const Type *Cur = this;
 
-    QualType Canon = Spec->getCanonicalTypeInternal();
-    if (Canon->getAs<TemplateSpecializationType>())
-      return QualType(this, 0);
-    return Canon->getDesugaredType();
+  while (true) {
+    switch (Cur->getTypeClass()) {
+#define ABSTRACT_TYPE(Class, Parent)
+#define TYPE(Class, Parent) \
+    case Class: { \
+      const Class##Type *Ty = cast<Class##Type>(Cur); \
+      if (!Ty->isSugared()) return Cur; \
+      Cur = Ty->desugar().getTypePtr(); \
+      break; \
+    }
+#include "clang/AST/TypeNodes.def"
+    }
   }
-  if (const QualifiedNameType *QualName  = dyn_cast<QualifiedNameType>(this)) {
-    if (ForDisplay) {
-      // If desugaring the type that the qualified name is referring to
-      // produces something interesting, that's our desugared type.
-      QualType NamedType = QualName->getNamedType().getDesugaredType();
-      if (NamedType != QualName->getNamedType())
-        return NamedType;
-    } else
-      return QualName->getNamedType().getDesugaredType();
-  }
-
-  return QualType(this, 0);
 }
 
 /// isVoidType - Helper method to determine if this is the 'void' type.
@@ -303,7 +288,7 @@
 
     // If this is a typedef for a structure type, strip the typedef off without
     // losing all typedef information.
-    return cast<RecordType>(getDesugaredType());
+    return cast<RecordType>(getUnqualifiedDesugaredType());
   }
   return 0;
 }
@@ -322,7 +307,7 @@
 
     // If this is a typedef for a union type, strip the typedef off without
     // losing all typedef information.
-    return cast<RecordType>(getDesugaredType());
+    return cast<RecordType>(getUnqualifiedDesugaredType());
   }
 
   return 0;
@@ -790,10 +775,18 @@
   return Qs.apply(CurType);
 }
 
+QualType TypedefType::desugar() const {
+  return getDecl()->getUnderlyingType();
+}
+
 TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
   : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
 }
 
+QualType TypeOfExprType::desugar() const {
+  return getUnderlyingExpr()->getType();
+}
+
 void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
                                       ASTContext &Context, Expr *E) {
   E->Profile(ID, Context, true);
diff --git a/lib/Analysis/RegionStore.cpp b/lib/Analysis/RegionStore.cpp
index f63e9d8..7a433dd 100644
--- a/lib/Analysis/RegionStore.cpp
+++ b/lib/Analysis/RegionStore.cpp
@@ -770,7 +770,7 @@
     return UnknownVal();
 
   // Strip off typedefs from the ArrayRegion's ValueType.
-  QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
+  QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
   ArrayType *AT = cast<ArrayType>(T);
   T = AT->getElementType();
 
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index 81f7283..3aae256 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -44,16 +44,23 @@
 
     // If this is a sugared type (like a typedef, typeof, etc), then unwrap one
     // level of the sugar so that the type is more obvious to the user.
-    QualType DesugaredTy = Ty.getDesugaredType(true);
+    QualType DesugaredTy = Ty.getDesugaredType();
 
     if (Ty != DesugaredTy &&
         // If the desugared type is a vector type, we don't want to expand it,
         // it will turn into an attribute mess. People want their "vec4".
         !isa<VectorType>(DesugaredTy) &&
 
-        // Don't aka just because we saw an elaborated type.
+        // Don't aka just because we saw an elaborated type...
         (!isa<ElaboratedType>(Ty) ||
-         cast<ElaboratedType>(Ty)->getUnderlyingType() != DesugaredTy) &&
+         cast<ElaboratedType>(Ty)->desugar() != DesugaredTy) &&
+
+        // ...or a qualified name type...
+        (!isa<QualifiedNameType>(Ty) ||
+         cast<QualifiedNameType>(Ty)->desugar() != DesugaredTy) &&
+
+        // ...or a non-dependent template specialization.
+        (!isa<TemplateSpecializationType>(Ty) || Ty->isDependentType()) &&
 
         // Don't desugar magic Objective-C types.
         Ty.getUnqualifiedType() != Context.getObjCIdType() &&
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 7aa0261..223662a 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -1267,7 +1267,7 @@
         assert(BaseClass && "ActOnMemInitializers - neither field or base");
         Diag(Member->getSourceLocation(),
              diag::error_multiple_base_initialization)
-          << BaseClass->getDesugaredType(true);
+          << QualType(BaseClass, 0);
       }
       Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer)
         << 0;
@@ -1336,7 +1336,7 @@
         Type *BaseClass = PrevMember->getBaseClass();
         Diag(PrevMember->getSourceLocation(),
              diag::warn_base_initialized)
-              << BaseClass->getDesugaredType(true);
+          << QualType(BaseClass, 0);
       } else {
         FieldDecl *Field = PrevMember->getMember();
         Diag(PrevMember->getSourceLocation(),
@@ -1352,7 +1352,7 @@
         Type *BaseClass = Member->getBaseClass();
         Diag(Member->getSourceLocation(),
              diag::note_fieldorbase_initialized_here) << 1
-          << BaseClass->getDesugaredType(true);
+          << QualType(BaseClass, 0);
       }
       for (curIndex = 0; curIndex < Last; curIndex++)
         if (MemberInCtorList == AllBaseOrMembers[curIndex])