Improvements to code-generation and semantic analysis of designated
initializers.

  - We now initialize unions properly when a member other than the
    first is named by a designated initializer.
  - We now provide proper semantic analysis and code generation for
    GNU array-range designators *except* that side effects will occur
    more than once. We warn about this.

llvm-svn: 63253
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2534a14..075a12c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -454,10 +454,17 @@
     if (Field->getType()->isIncompleteArrayType())
       break;
 
-    if (Field->getIdentifier() == 0) {
-      // Initializers can't initialize unnamed fields, e.g. "int : 20;"
+    if (Field->isUnnamedBitfield())
       continue;
-    }
+
+    // When we're coping with C99 designated initializers into a
+    // union, find the field that has the same type as the expression
+    // we're initializing the union with.
+    if (isUnion && CurInitVal < NumInitElements && 
+        (CGF.getContext().getCanonicalType(Field->getType()) != 
+           CGF.getContext().getCanonicalType(E->getInit(CurInitVal)->getType())))
+      continue;
+
     // FIXME: volatility
     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, isUnion,0);
     if (CurInitVal < NumInitElements) {
@@ -471,8 +478,6 @@
     }
 
     // Unions only initialize one field.
-    // (FIXME: things can get weird with designators, but they aren't
-    // supported yet.)
     if (isUnion)
       break;
   }
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 87868ed..b05048c 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -246,12 +246,22 @@
     // first field
     int FieldNo = 0; // Field no in RecordDecl
     FieldDecl* curField = 0;
+    bool sawAnyFields = false;
     for (RecordDecl::field_iterator Field = RD->field_begin(),
                                  FieldEnd = RD->field_end();
          Field != FieldEnd; ++Field) {
       curField = *Field;
       FieldNo++;
-      if (curField->getIdentifier())
+
+      if (curField->isUnnamedBitfield())
+        continue;
+
+      // If we have an initializer, find the field whose type is the
+      // same as that initializer. This 
+      sawAnyFields = true;
+      if (ILE->getNumInits() > 0 &&
+          CGM.getContext().getCanonicalType(curField->getType()) ==
+            CGM.getContext().getCanonicalType(ILE->getInit(0)->getType()))
         break;
     }
 
diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp b/clang/lib/CodeGen/CodeGenTypes.cpp
index aac6e8d..edd1608 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -518,6 +518,7 @@
        Field != FieldEnd; ++Field) {
     // The offset should usually be zero, but bitfields could be strange
     uint64_t offset = RL.getFieldOffset(curField);
+    CGT.ConvertTypeRecursive(Field->getType());
 
     if (Field->isBitField()) {
       Expr *BitWidth = Field->getBitWidth();