Fix several bugs in array -> pointer decomposition.

First, we got several CVR propagation cases wrong, which Eli pointed
out in PR2039.

Second, we didn't propagate address space qualifiers correctly, leading
to incorrect lowering of code in CodeGen/address-space.c.

Third, we didn't uniformly propagate the specifier in the array to the
pointer ("int[restrict 4]" -> "int *restrict").

This adds an ASTContext::getArrayDecayedType member that handles the 
non-trivial logic for this seemingly simple operation.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49078 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 902f422..b9b1af1 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -929,6 +929,71 @@
   return IntTy; 
 }
 
+//===----------------------------------------------------------------------===//
+//                              Type Operators
+//===----------------------------------------------------------------------===//
+
+/// getArrayDecayedType - Return the properly qualified result of decaying the
+/// specified array type to a pointer.  This operation is non-trivial when
+/// handling typedefs etc.  The canonical type of "T" must be an array type,
+/// this returns a pointer to a properly qualified element of the array.
+///
+/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
+QualType ASTContext::getArrayDecayedType(QualType Ty) {
+  // Handle the common case where typedefs are not involved directly.
+  QualType EltTy;
+  unsigned ArrayQuals = 0;
+  unsigned PointerQuals = 0;
+  if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
+    // Since T "isa" an array type, it could not have had an address space
+    // qualifier, just CVR qualifiers.  The properly qualified element pointer
+    // gets the union of the CVR qualifiers from the element and the array, and
+    // keeps any address space qualifier on the element type if present.
+    EltTy = AT->getElementType();
+    ArrayQuals = Ty.getCVRQualifiers();
+    PointerQuals = AT->getIndexTypeQualifier();
+  } else {
+    // Otherwise, we have an ASQualType or a typedef, etc.  Make sure we don't
+    // lose qualifiers when dealing with typedefs. Example:
+    //   typedef int arr[10];
+    //   void test2() {
+    //     const arr b;
+    //     b[4] = 1;
+    //   }
+    //
+    // The decayed type of b is "const int*" even though the element type of the
+    // array is "int".
+    QualType CanTy = Ty.getCanonicalType();
+    const ArrayType *PrettyArrayType = Ty->getAsArrayType();
+    assert(PrettyArrayType && "Not an array type!");
+    
+    // Get the element type with 'getAsArrayType' so that we don't lose any
+    // typedefs in the element type of the array.
+    EltTy = PrettyArrayType->getElementType();
+
+    // If the array was address-space qualifier, make sure to ASQual the element
+    // type.  We can just grab the address space from the canonical type.
+    if (unsigned AS = CanTy.getAddressSpace())
+      EltTy = getASQualType(EltTy, AS);
+    
+    // To properly handle [multiple levels of] typedefs, typeof's etc, we take
+    // the CVR qualifiers directly from the canonical type, which is guaranteed
+    // to have the full set unioned together.
+    ArrayQuals = CanTy.getCVRQualifiers();
+    PointerQuals = PrettyArrayType->getIndexTypeQualifier();
+  }
+  
+  // Apply any CVR qualifiers from the array type.
+  EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
+
+  QualType PtrTy = getPointerType(EltTy);
+
+  // int x[restrict 4] ->  int *restrict
+  PtrTy = PtrTy.getQualifiedType(PointerQuals);
+
+  return PtrTy;
+}
+
 /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
 /// routine will assert if passed a built-in type that isn't an integer or enum.
 static int getIntegerRank(QualType t) {