VariableArrayTypes (VLAs) without a size expression are now uniqued
and inserted into a FoldingSet owned by ASTContext.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43482 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp
index 93d4232..72511bd 100644
--- a/AST/ASTContext.cpp
+++ b/AST/ASTContext.cpp
@@ -451,11 +451,49 @@
 QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
                                           ArrayType::ArraySizeModifier ASM,
                                           unsigned EltTypeQuals) {
-  // Since we don't unique expressions, it isn't possible to unique VLA's.
-  ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, 
-                                         ASM, EltTypeQuals);
-  Types.push_back(New);
-  return QualType(New, 0);
+  if (NumElts) {
+    // Since we don't unique expressions, it isn't possible to unique VLA's
+    // that have an expression provided for their size.
+    
+    ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, 
+                                           ASM, EltTypeQuals);
+    
+    // FIXME: Also add non-uniqued VLAs into a list of their own.
+    Types.push_back(New);
+    return QualType(New, 0);
+  }
+  else {
+    // No size is provided for the VLA.  These we can unique.
+    llvm::FoldingSetNodeID ID;
+    VariableArrayType::Profile(ID, EltTy);
+    
+    void *InsertPos = 0;
+    if (VariableArrayType *ATP = 
+         IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
+      return QualType(ATP, 0);
+    
+    // If the element type isn't canonical, this won't be a canonical type
+    // either, so fill in the canonical type field.
+    QualType Canonical;
+    
+    if (!EltTy->isCanonical()) {
+      Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
+                                       ASM, EltTypeQuals);
+      
+      // Get the new insert position for the node we care about.
+      VariableArrayType *NewIP =
+        IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
+      
+      assert(NewIP == 0 && "Shouldn't be in the map!");
+    }
+    
+    VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, 
+                                                   ASM, EltTypeQuals);
+    
+    IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
+    Types.push_back(New);
+    return QualType(New, 0);            
+  }
 }
 
 /// getVectorType - Return the unique reference to a vector type of