add a common base class "PointerLikeType" for PointerType and ReferenceType,
allowing them to be treated the same in some contexts.  A suggestion for a
better name is welcome :)


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49100 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index ce2e58f..d716917 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -281,7 +281,7 @@
     // of the referenced type." C++98 5.3.3p2: expr.sizeof.
     // FIXME: This is wrong for struct layout: a reference in a struct has
     // pointer size.
-    return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType());
+    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
     
   case Type::Complex: {
     // Complex types have the same alignment as their elements, but twice the
@@ -1725,12 +1725,12 @@
   QualType ltype = lhs;
 
   if (lhs->isReferenceType())
-    ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
+    ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
 
   QualType rtype = rhs;
 
   if (rhs->isReferenceType())
-    rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
+    rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
 
   return typesAreCompatible(ltype, rtype);
 }
@@ -1817,9 +1817,9 @@
   // designates the object or function denoted by the reference, and the
   // expression is an lvalue.
   if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
-    lcanon = RT->getReferenceeType();
+    lcanon = RT->getPointeeType();
   if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
-    rcanon = RT->getReferenceeType();
+    rcanon = RT->getPointeeType();
   
   Type::TypeClass LHSClass = lcanon->getTypeClass();
   Type::TypeClass RHSClass = rcanon->getTypeClass();
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index 9411cd8..40b376b 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -155,6 +155,24 @@
   return getDesugaredType()->getAsFunctionType();
 }
 
+const PointerLikeType *Type::getAsPointerLikeType() const {
+  // If this is directly a pointer-like type, return it.
+  if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
+    return PTy;
+  
+  // If the canonical form of this type isn't the right kind, reject it.
+  if (!isa<PointerLikeType>(CanonicalType)) {
+    // Look through type qualifiers
+    if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
+      return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
+    return 0;
+  }
+  
+  // If this is a typedef for a pointer type, strip the typedef off without
+  // losing all typedef information.
+  return getDesugaredType()->getAsPointerLikeType();
+}
+
 const PointerType *Type::getAsPointerType() const {
   // If this is directly a pointer type, return it.
   if (const PointerType *PTy = dyn_cast<PointerType>(this))
@@ -796,10 +814,10 @@
   
   // Handle things like 'int (*A)[4];' correctly.
   // FIXME: this should include vectors, but vectors use attributes I guess.
-  if (isa<ArrayType>(PointeeType.getTypePtr()))
+  if (isa<ArrayType>(getPointeeType()))
     S = '(' + S + ')';
   
-  PointeeType.getAsStringInternal(S);
+  getPointeeType().getAsStringInternal(S);
 }
 
 void ReferenceType::getAsStringInternal(std::string &S) const {
@@ -807,10 +825,10 @@
   
   // Handle things like 'int (&A)[4];' correctly.
   // FIXME: this should include vectors, but vectors use attributes I guess.
-  if (isa<ArrayType>(ReferenceeType.getTypePtr()))
+  if (isa<ArrayType>(getPointeeType()))
     S = '(' + S + ')';
   
-  ReferenceeType.getAsStringInternal(S);
+  getPointeeType().getAsStringInternal(S);
 }
 
 void ConstantArrayType::getAsStringInternal(std::string &S) const {