SemaInit.cpp revision eeae8f072748affce25ab4064982626361293390
10cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===//
20cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
30cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//                     The LLVM Compiler Infrastructure
40cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
50cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff// This file is distributed under the University of Illinois Open Source
60cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff// License. See LICENSE.TXT for details.
70cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
80cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//===----------------------------------------------------------------------===//
90cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
10dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// This file implements semantic analysis for initializers. The main entry
11dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// point is Sema::CheckInitList(), but all of the work is performed
12dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// within the InitListChecker class.
13dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//
148b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattner// This file also implements Sema::CheckInitializerTypes.
150cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
160cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//===----------------------------------------------------------------------===//
170cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
180cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff#include "Sema.h"
1905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor#include "clang/Parse/Designator.h"
200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff#include "clang/AST/ASTContext.h"
2179e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner#include "clang/AST/ExprObjC.h"
22c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor#include <map>
2305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregorusing namespace clang;
240cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
25dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
26dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Sema Initialization Checking
27dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
28dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
2979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattnerstatic Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
308879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  const ArrayType *AT = Context.getAsArrayType(DeclType);
318879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (!AT) return 0;
328879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
338879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // See if this is a string literal or @encode.
348879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  Init = Init->IgnoreParens();
358879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
368879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Handle @encode, which is a narrow string.
378879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
388879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
398879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
408879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Otherwise we can only handle string literals.
418879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  StringLiteral *SL = dyn_cast<StringLiteral>(Init);
42220b6369d7717bfe6894b46cef055d3e763827f2Chris Lattner  if (SL == 0) return 0;
438879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
448879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // char array can be initialized with a narrow string.
458879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Only allow char x[] = "foo";  not char x[] = L"foo";
468879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (!SL->isWide())
478879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return AT->getElementType()->isCharType() ? Init : 0;
488879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
498879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // wchar_t array can be initialized with a wide string: C99 6.7.8p15:
508879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // "An array with element type compatible with wchar_t may be initialized by a
518879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // wide string literal, optionally enclosed in braces."
5219753cfa6059b237880a91f21ef58f2d8984845fChris Lattner  if (Context.typesAreCompatible(Context.getWCharType(), AT->getElementType()))
538879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    // Only allow wchar_t x[] = L"foo";  not wchar_t x[] = "foo";
548879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
558879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
56dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  return 0;
57dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
58dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
5995e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattnerstatic bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
6095e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner                                   bool DirectInit, Sema &S) {
61dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // Get the type before calling CheckSingleAssignmentConstraints(), since
62dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // it can promote the expression.
63dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  QualType InitType = Init->getType();
64dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
6595e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  if (S.getLangOptions().CPlusPlus) {
66dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // FIXME: I dislike this error message. A lot.
6795e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner    if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit))
6895e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner      return S.Diag(Init->getSourceRange().getBegin(),
6995e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner                    diag::err_typecheck_convert_incompatible)
7095e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner        << DeclType << Init->getType() << "initializing"
7195e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner        << Init->getSourceRange();
72dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return false;
73dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
74dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
7595e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  Sema::AssignConvertType ConvTy =
7695e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner    S.CheckSingleAssignmentConstraints(DeclType, Init);
7795e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
78dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                  InitType, Init, "initializing");
79dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
80dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
8179e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattnerstatic void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
8279e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  // Get the length of the string as parsed.
8379e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  uint64_t StrLength =
8479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
8579e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner
86dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
8779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  const ArrayType *AT = S.Context.getAsArrayType(DeclT);
88dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
89dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // C99 6.7.8p14. We have an array of character type with unknown size
90dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // being initialized to a string literal.
91dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    llvm::APSInt ConstVal(32);
9219da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    ConstVal = StrLength;
93dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // Return a new array type (C99 6.7.8p22).
94f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner    DeclT = S.Context.getConstantArrayType(IAT->getElementType(), ConstVal,
95f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner                                           ArrayType::Normal, 0);
9619da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    return;
97dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
9819da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner
9919da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
10019da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner
10119da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  // C99 6.7.8p14. We have an array of character type with known size.  However,
10219da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  // the size may be smaller or larger than the string we are initializing.
10319da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  // FIXME: Avoid truncation for 64-bit length strings.
10479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  if (StrLength-1 > CAT->getSize().getZExtValue())
10519da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    S.Diag(Str->getSourceRange().getBegin(),
10619da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner           diag::warn_initializer_string_for_char_array_too_long)
10719da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner      << Str->getSourceRange();
10819da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner
10919da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  // Set the type to the actual size that we are initializing.  If we have
11019da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  // something like:
11119da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  //   char x[1] = "foo";
11219da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner  // then this will set the string literal's type to char[1].
113f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner  Str->setType(DeclT);
114dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
115dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
116dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattnerbool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
117dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                 SourceLocation InitLoc,
118dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                 DeclarationName InitEntity,
119dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                 bool DirectInit) {
120dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (DeclType->isDependentType() || Init->isTypeDependent())
121dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return false;
122dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
123dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // C++ [dcl.init.ref]p1:
1247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  //   A variable declared to be a T& or T&&, that is "reference to type T"
125dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  //   (8.3.2), shall be initialized by an object, or function, of
126dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  //   type T or by an object that can be converted into a T.
127dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (DeclType->isReferenceType())
128dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return CheckReferenceInit(Init, DeclType, 0, false, DirectInit);
129dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
130dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // C99 6.7.8p3: The type of the entity to be initialized shall be an array
131dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // of unknown size ("[]") or an object type that is not a variable array type.
132dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
133dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return Diag(InitLoc,  diag::err_variable_object_no_init)
134dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    << VAT->getSizeExpr()->getSourceRange();
135dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
136dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
137dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (!InitList) {
138dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // FIXME: Handle wide strings
13979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
14079e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner      CheckStringInit(Str, DeclType, *this);
14119da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner      return false;
14219da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    }
143dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
144dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // C++ [dcl.init]p14:
145dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    //   -- If the destination type is a (possibly cv-qualified) class
146dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    //      type:
147dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
148dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      QualType DeclTypeC = Context.getCanonicalType(DeclType);
149dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      QualType InitTypeC = Context.getCanonicalType(Init->getType());
150dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
151dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //   -- If the initialization is direct-initialization, or if it is
152dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      copy-initialization where the cv-unqualified version of the
153dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      source type is the same class as, or a derived class of, the
154dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      class of the destination, constructors are considered.
155dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
156dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner          IsDerivedFrom(InitTypeC, DeclTypeC)) {
157dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        CXXConstructorDecl *Constructor
158dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        = PerformInitializationByConstructor(DeclType, &Init, 1,
159dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                             InitLoc, Init->getSourceRange(),
160dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                             InitEntity,
161dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                             DirectInit? IK_Direct : IK_Copy);
162dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return Constructor == 0;
163dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      }
164dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
165dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //   -- Otherwise (i.e., for the remaining copy-initialization
166dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      cases), user-defined conversion sequences that can
167dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      convert from the source type to the destination type or
168dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      (when a conversion function is used) to a derived class
169dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      thereof are enumerated as described in 13.3.1.4, and the
170dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      best one is chosen through overload resolution
171dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      (13.3). If the conversion cannot be done or is
172dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      ambiguous, the initialization is ill-formed. The
173dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      function selected is called with the initializer
174dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      expression as its argument; if the function is a
175dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      constructor, the call initializes a temporary of the
176dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      destination type.
177dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      // FIXME: We're pretending to do copy elision here; return to
178dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      // this when we have ASTs for such things.
179dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      if (!PerformImplicitConversion(Init, DeclType, "initializing"))
180dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return false;
181dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
182dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      if (InitEntity)
183dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return Diag(InitLoc, diag::err_cannot_initialize_decl)
184dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
185dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << Init->getType() << Init->getSourceRange();
186dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      else
187dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
188dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
189dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << Init->getType() << Init->getSourceRange();
190dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    }
191dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
192dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // C99 6.7.8p16.
193dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    if (DeclType->isArrayType())
194dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      return Diag(Init->getLocStart(), diag::err_array_init_list_required)
195dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      << Init->getSourceRange();
196dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
19795e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner    return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
198dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
199dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
200dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  bool hadError = CheckInitList(InitList, DeclType);
201dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  Init = InitList;
202dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  return hadError;
203dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
204dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
205dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
206dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Semantic checking for initializer lists.
207dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
208dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
2099e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// @brief Semantic checking for initializer lists.
2109e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
2119e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// The InitListChecker class contains a set of routines that each
2129e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// handle the initialization of a certain kind of entity, e.g.,
2139e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arrays, vectors, struct/union types, scalars, etc. The
2149e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// InitListChecker itself performs a recursive walk of the subobject
2159e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure of the type to be initialized, while stepping through
2169e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// the initializer list one element at a time. The IList and Index
2179e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// parameters to each of the Check* routines contain the active
2189e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// (syntactic) initializer list and the index into that initializer
2199e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list that represents the current initializer. Each routine is
2209e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// responsible for moving that Index forward as it consumes elements.
2219e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
2229e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// Each Check* routine also has a StructuredList/StructuredIndex
2239e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arguments, which contains the current the "structured" (semantic)
2249e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initializer list and the index into that initializer list where we
2259e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// are copying initializers as we map them over to the semantic
2269e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list. Once we have completed our recursive walk of the subobject
2279e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure, we will have constructed a full semantic initializer
2289e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list.
2299e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
2309e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// C99 designators cause changes in the initializer list traversal,
2319e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// because they make the initialization "jump" into a specific
2329e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// subobject and then continue the initialization from that
2339e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// point. CheckDesignatedInitializer() recursively steps into the
2349e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// designated subobject and manages backing out the recursion to
2359e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initialize the subobjects after the one designated.
2368b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattnernamespace {
237c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorclass InitListChecker {
2380820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  Sema &SemaRef;
239c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool hadError;
240c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
241c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *FullyStructuredList;
242c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
243c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
2449e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
245eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
246eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
247c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckExplicitInitList(InitListExpr *IList, QualType &T,
2489e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
249eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
250eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
251c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
252c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             bool SubobjectIsDesignatorContext,
253c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             unsigned &Index,
2549e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
255eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
256eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
257c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckSubElementType(InitListExpr *IList, QualType ElemType,
258c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                           unsigned &Index,
2599e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           InitListExpr *StructuredList,
2609e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           unsigned &StructuredIndex);
261930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  void CheckScalarType(InitListExpr *IList, QualType DeclType,
262c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                       unsigned &Index,
2639e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2649e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
265930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  void CheckReferenceType(InitListExpr *IList, QualType DeclType,
266930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &Index,
267930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          InitListExpr *StructuredList,
268930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &StructuredIndex);
269c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
2709e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2719e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
272c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
273c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             RecordDecl::field_iterator Field,
274c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             bool SubobjectIsDesignatorContext, unsigned &Index,
2759e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
276eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
277eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
278c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckArrayType(InitListExpr *IList, QualType &DeclType,
279c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                      llvm::APSInt elementIndex,
280c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                      bool SubobjectIsDesignatorContext, unsigned &Index,
2819e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      InitListExpr *StructuredList,
2829e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      unsigned &StructuredIndex);
283c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
284c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  DesignatedInitExpr::designators_iterator D,
285c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  QualType &CurrentObjectType,
286c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  RecordDecl::field_iterator *NextField,
287c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  llvm::APSInt *NextElementIndex,
288c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &Index,
289c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  InitListExpr *StructuredList,
290c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &StructuredIndex,
291eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool FinishSubobjectInit,
292eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool TopLevelObject);
293c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
294c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           QualType CurrentObjectType,
295c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           InitListExpr *StructuredList,
296c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           unsigned StructuredIndex,
297c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           SourceRange InitRange);
2989e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor  void UpdateStructuredListElement(InitListExpr *StructuredList,
2999e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                                   unsigned &StructuredIndex,
300c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                   Expr *expr);
301c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numArrayElements(QualType DeclType);
302c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numStructUnionElements(QualType DeclType);
303930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
304930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  void FillInValueInitializations(InitListExpr *ILE);
305c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorpublic:
3060820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
307c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool HadError() { return hadError; }
308c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
309c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // @brief Retrieves the fully-structured initializer list used for
310c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // semantic analysis and code generation.
311c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
312c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor};
3138b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattner} // end anonymous namespace
31468355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
3154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Recursively replaces NULL values within the given initializer list
3164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// with expressions that perform value-initialization of the
3174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// appropriate type.
318930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
3190820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  assert((ILE->getType() != SemaRef.Context.VoidTy) &&
320930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor         "Should not have void type");
32187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  SourceLocation Loc = ILE->getSourceRange().getBegin();
32287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (ILE->getSyntacticForm())
32387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
32487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
3254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
3264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned Init = 0, NumInits = ILE->getNumInits();
3274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(),
3284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                 FieldEnd = RType->getDecl()->field_end();
3294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor         Field != FieldEnd; ++Field) {
3304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->isUnnamedBitfield())
3314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        continue;
3324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
33387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      if (Init >= NumInits || !ILE->getInit(Init)) {
334930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        if (Field->getType()->isReferenceType()) {
335930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          // C++ [dcl.init.aggr]p9:
336930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          //   If an incomplete or empty initializer-list leaves a
337930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          //   member of reference type uninitialized, the program is
338930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          //   ill-formed.
3390820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner          SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
340930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor            << Field->getType()
341930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor            << ILE->getSyntacticForm()->getSourceRange();
3420820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner          SemaRef.Diag(Field->getLocation(),
343930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                        diag::note_uninit_reference_member);
344930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          hadError = true;
34587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          return;
3460820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
34787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          hadError = true;
34887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          return;
349930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        }
35087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
35187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        // FIXME: If value-initialization involves calling a
35287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        // constructor, should we make that call explicit in the
35387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        // representation (even when it means extending the
35487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        // initializer list)?
35587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        if (Init < NumInits && !hadError)
35687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          ILE->setInit(Init,
3570820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner              new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
35887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      } else if (InitListExpr *InnerILE
3593498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor                 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
360930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        FillInValueInitializations(InnerILE);
3614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++Init;
362930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
363930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Only look at the first initialization of a union.
364930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      if (RType->getDecl()->isUnion())
365930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        break;
3664c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    }
3674c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3684c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
3694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
3704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  QualType ElementType;
3724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
37387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumInits = ILE->getNumInits();
37487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumElements = NumInits;
3750820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
3764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = AType->getElementType();
37787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
37887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      NumElements = CAType->getSize().getZExtValue();
37987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
3804c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = VType->getElementType();
38187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    NumElements = VType->getNumElements();
38287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  } else
3834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = ILE->getType();
3844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
38587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  for (unsigned Init = 0; Init != NumElements; ++Init) {
38687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (Init >= NumInits || !ILE->getInit(Init)) {
3870820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
38887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        hadError = true;
38987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        return;
39087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      }
39187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
39287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      // FIXME: If value-initialization involves calling a
39387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      // constructor, should we make that call explicit in the
39487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      // representation (even when it means extending the
39587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      // initializer list)?
39687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      if (Init < NumInits && !hadError)
39787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        ILE->setInit(Init,
3980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                     new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
39987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    }
40068355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner    else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init)))
401930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      FillInValueInitializations(InnerILE);
4024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
4034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
4044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
40568355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
4060820254f97bb8925d933a3664ea1c6fca3997b97Chris LattnerInitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
4070820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  : SemaRef(S) {
4080cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  hadError = false;
409c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
410b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  unsigned newIndex = 0;
4114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned newStructuredIndex = 0;
4124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  FullyStructuredList
413ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
414eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
415eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        /*TopLevelObject=*/true);
416c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
417930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  if (!hadError)
418930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    FillInValueInitializations(FullyStructuredList);
4190cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4210cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numArrayElements(QualType DeclType) {
422638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  // FIXME: use a proper constant
423638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  int maxElements = 0x7FFFFFFF;
424c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT =
4250820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
4260cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = static_cast<int>(CAT->getSize().getZExtValue());
4270cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
4280cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  return maxElements;
4290cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4300cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4310cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numStructUnionElements(QualType DeclType) {
4320cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
4334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int InitializableMembers = 0;
4344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  for (RecordDecl::field_iterator Field = structDecl->field_begin(),
4354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                               FieldEnd = structDecl->field_end();
4364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor       Field != FieldEnd; ++Field) {
4374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if ((*Field)->getIdentifier() || !(*Field)->isBitField())
4384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++InitializableMembers;
4394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
44039ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis  if (structDecl->isUnion())
441f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman    return std::min(InitializableMembers, 1);
442f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman  return InitializableMembers - structDecl->hasFlexibleArrayMember();
4430cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4440cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4450cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
4464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType T, unsigned &Index,
4474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
448eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
449eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
4500cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  int maxElements = 0;
4510cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4520cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (T->isArrayType())
4530cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numArrayElements(T);
4540cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else if (T->isStructureType() || T->isUnionType())
4550cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numStructUnionElements(T);
456b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  else if (T->isVectorType())
457b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    maxElements = T->getAsVectorType()->getNumElements();
4580cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else
4590cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    assert(0 && "CheckImplicitInitList(): Illegal type");
460b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
461402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  if (maxElements == 0) {
4620820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
463402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman                  diag::err_implicit_empty_initializer);
4644c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
465402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    hadError = true;
466402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    return;
467402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  }
468402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman
4694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Build a structured initializer list corresponding to this subobject.
4704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitListExpr *StructuredSubobjectInitList
4714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
4724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                 StructuredIndex,
473ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor          SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
474ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                      ParentIList->getSourceRange().getEnd()));
4754c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned StructuredSubobjectInitIndex = 0;
4764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
4774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Check the element types and build the structural subobject.
47887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned StartIndex = Index;
4794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  CheckListElementTypes(ParentIList, T, false, Index,
4804c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredSubobjectInitList,
481eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        StructuredSubobjectInitIndex,
482eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        TopLevelObject);
48387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
484a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  StructuredSubobjectInitList->setType(T);
485a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor
486ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor  // Update the structured sub-object initializer so that it's ending
48787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  // range corresponds with the end of the last initializer it used.
48887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (EndIndex < ParentIList->getNumInits()) {
48987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    SourceLocation EndLoc
49087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
49187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    StructuredSubobjectInitList->setRBraceLoc(EndLoc);
49287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
4930cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4940cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
495a647caad2dec67ac25b763f06237cfe3c3968b51Steve Naroffvoid InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
4964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
4974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
498eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
499eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
500c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
5014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  SyntacticToSemantic[IList] = StructuredList;
5024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList->setSyntacticForm(IList);
5034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  CheckListElementTypes(IList, T, true, Index, StructuredList,
504eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        StructuredIndex, TopLevelObject);
505a647caad2dec67ac25b763f06237cfe3c3968b51Steve Naroff  IList->setType(T);
5064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList->setType(T);
507638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (hadError)
508638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
509c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
510638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (Index < IList->getNumInits()) {
511c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    // We have leftover initializers
512c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    if (IList->getNumInits() > 0 &&
5130820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        IsStringInit(IList->getInit(Index), T, SemaRef.Context)) {
5147c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
5150820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.getLangOptions().CPlusPlus)
5167c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor        DK = diag::err_excess_initializers_in_char_array_initializer;
517bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      // Special-case
5180820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
519dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getInit(Index)->getSourceRange();
520c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman      hadError = true;
521d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    } else if (!T->isIncompleteType()) {
522b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // Don't complain for incomplete types, since we'll get an error
523b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // elsewhere
524eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType CurrentObjectType = StructuredList->getType();
525eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      int initKind =
526eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isArrayType()? 0 :
527eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isVectorType()? 1 :
528eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isScalarType()? 2 :
529eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isUnionType()? 3 :
530eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        4;
5317c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
5327c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers;
5330820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.getLangOptions().CPlusPlus)
5347c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor          DK = diag::err_excess_initializers;
5357c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
5360820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
537eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        << initKind << IList->getInit(Index)->getSourceRange();
538c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    }
539c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  }
540cda25a977e4b7fe4e080b87586410eaeab7b62f6Eli Friedman
541638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (T->isScalarType())
5420820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
543dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << IList->getSourceRange();
5440cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5450cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
546b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedmanvoid InitListChecker::CheckListElementTypes(InitListExpr *IList,
547b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman                                            QualType &DeclType,
54887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            bool SubobjectIsDesignatorContext,
5494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
5504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
551eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
552eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
553c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (DeclType->isScalarType()) {
5546fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
555c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (DeclType->isVectorType()) {
5564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
557d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor  } else if (DeclType->isAggregateType()) {
558d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor    if (DeclType->isRecordType()) {
55987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
56087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
5614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                            SubobjectIsDesignatorContext, Index,
562eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                            StructuredList, StructuredIndex,
563eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                            TopLevelObject);
56487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    } else if (DeclType->isArrayType()) {
565f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      llvm::APSInt Zero(
5660820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                      SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
567f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor                      false);
5684c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
5694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                     StructuredList, StructuredIndex);
57087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
5710cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    else
5724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      assert(0 && "Aggregate that isn't a structure or array?!");
573613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff  } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
574613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff    // This type is invalid, issue a diagnostic.
5754c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
5760820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
577d162584991885ab004a02573a73ce06422b921fcChris Lattner      << DeclType;
578d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    hadError = true;
579930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isRecordType()) {
580930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init]p14:
581930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   [...] If the class is an aggregate (8.5.1), and the initializer
582930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   is a brace-enclosed list, see 8.5.1.
583930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //
584930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Note: 8.5.1 is handled below; here, we diagnose the case where
585930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // we have an initializer list and a destination type that is not
586930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // an aggregate.
587930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // FIXME: In C++0x, this is yet another form of initialization.
5880820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
589930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << DeclType << IList->getSourceRange();
590930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
591930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isReferenceType()) {
592930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
5930cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  } else {
5940cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // In C, all types are either scalars or aggregates, but
5950cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // additional handling is needed here for C++ (and possibly others?).
5960cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    assert(0 && "Unsupported initializer type");
5970cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
5980cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5990cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
600b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedmanvoid InitListChecker::CheckSubElementType(InitListExpr *IList,
601b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman                                          QualType ElemType,
6024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &Index,
6034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          InitListExpr *StructuredList,
6044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &StructuredIndex) {
6056fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor  Expr *expr = IList->getInit(Index);
606c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
607c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    unsigned newIndex = 0;
6084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned newStructuredIndex = 0;
6094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    InitListExpr *newStructuredList
6104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      = getStructuredSubobjectInit(IList, Index, ElemType,
6114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                   StructuredList, StructuredIndex,
6124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                   SubInitList->getSourceRange());
6134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckExplicitInitList(SubInitList, ElemType, newIndex,
6144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          newStructuredList, newStructuredIndex);
6154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
6164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
61779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
61879e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    CheckStringInit(Str, ElemType, SemaRef);
619f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner    UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
6204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
621c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (ElemType->isScalarType()) {
6226fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
623930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (ElemType->isReferenceType()) {
624930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
625b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  } else {
6260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (SemaRef.getLangOptions().CPlusPlus) {
627930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // C++ [dcl.init.aggr]p12:
628930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   All implicit type conversions (clause 4) are considered when
629930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   initializing the aggregate member with an ini- tializer from
630930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   an initializer-list. If the initializer can initialize a
631930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   member, the member is initialized. [...]
632930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ImplicitConversionSequence ICS
6330820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        = SemaRef.TryCopyInitialization(expr, ElemType);
634930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
6350820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
636930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                               "initializing"))
637930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          hadError = true;
638930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
639930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        ++Index;
640930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        return;
641930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      }
642930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
643930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Fall through for subaggregate initialization
644930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    } else {
645930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // C99 6.7.8p13:
646930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //
647930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   The initializer for a structure or union object that has
648930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   automatic storage duration shall be either an initializer
649930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   list as described below, or a single expression that has
650930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   compatible structure or union type. In the latter case, the
651930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   initial value of the object, including unnamed members, is
652930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   that of the expression.
6530820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      QualType ExprType = SemaRef.Context.getCanonicalType(expr->getType());
6540820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      QualType ElemTypeCanon = SemaRef.Context.getCanonicalType(ElemType);
6550820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.Context.typesAreCompatible(ExprType.getUnqualifiedType(),
656930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                          ElemTypeCanon.getUnqualifiedType())) {
657930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
658930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        ++Index;
659930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        return;
660930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      }
661930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
662930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Fall through for subaggregate initialization
663930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
664930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
665930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init.aggr]p12:
666930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //
667930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   [...] Otherwise, if the member is itself a non-empty
668930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   subaggregate, brace elision is assumed and the initializer is
669930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   considered for the initialization of the first member of
670930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   the subaggregate.
671930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (ElemType->isAggregateType() || ElemType->isVectorType()) {
672930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      CheckImplicitInitList(IList, ElemType, Index, StructuredList,
673930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                            StructuredIndex);
674930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
675930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    } else {
676930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // We cannot initialize this element, so let
677930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // PerformCopyInitialization produce the appropriate diagnostic.
6780820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
679930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
680930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
681930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
682930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
683930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
684b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman}
685b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
686930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
6876fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor                                      unsigned &Index,
6884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
6894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
6900cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
6916fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    Expr *expr = IList->getInit(Index);
692c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    if (isa<InitListExpr>(expr)) {
6930820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
694dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                    diag::err_many_braces_around_scalar_init)
695dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getSourceRange();
696bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      hadError = true;
697bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      ++Index;
6984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
699bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      return;
70005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    } else if (isa<DesignatedInitExpr>(expr)) {
7010820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(expr->getSourceRange().getBegin(),
70205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                    diag::err_designator_for_scalar_init)
70305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        << DeclType << expr->getSourceRange();
70405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      hadError = true;
70505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Index;
7064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
70705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      return;
7080cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
70905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
710c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
7110820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
712bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      hadError = true; // types weren't compatible.
71305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else if (savExpr != expr) {
714c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman      // The type was promoted, update initializer list.
7156fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      IList->setInit(Index, expr);
71605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
7174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (hadError)
7184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
7194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    else
7204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
7210cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    ++Index;
722bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman  } else {
7230820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
724dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << IList->getSourceRange();
725bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    hadError = true;
7264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
7274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
728bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    return;
7290cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
7300cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
7310cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
732930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
733930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &Index,
734930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         InitListExpr *StructuredList,
735930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &StructuredIndex) {
736930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  if (Index < IList->getNumInits()) {
737930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    Expr *expr = IList->getInit(Index);
738930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (isa<InitListExpr>(expr)) {
7390820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
740930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        << DeclType << IList->getSourceRange();
741930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
742930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
743930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
744930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      return;
745930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
746930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
747930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
7480820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (SemaRef.CheckReferenceInit(expr, DeclType))
749930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
750930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    else if (savExpr != expr) {
751930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // The type was promoted, update initializer list.
752930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      IList->setInit(Index, expr);
753930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
754930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (hadError)
755930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
756930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    else
757930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
758930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
759930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else {
760930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // FIXME: It would be wonderful if we could point at the actual
761930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // member. In general, it would be useful to pass location
762930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // information down the stack, so that we know the location (or
763930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // decl) of the "current object" being initialized.
7640820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(),
765930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                  diag::err_init_reference_member_uninitialized)
766930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << DeclType
767930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << IList->getSourceRange();
768930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
769930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
770930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++StructuredIndex;
771930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    return;
772930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
773930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor}
774930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
7750cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
7764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
7774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
7784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
7790cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
7800cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    const VectorType *VT = DeclType->getAsVectorType();
7810cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    int maxElements = VT->getNumElements();
7820cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    QualType elementType = VT->getElementType();
7830cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
7840cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    for (int i = 0; i < maxElements; ++i) {
7850cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      // Don't attempt to go past the end of the init list
7860cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      if (Index >= IList->getNumInits())
7870cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff        break;
7886fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      CheckSubElementType(IList, elementType, Index,
7894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          StructuredList, StructuredIndex);
7900cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
7910cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
7920cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
7930cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
7940cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
79587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                     llvm::APSInt elementIndex,
79687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                     bool SubobjectIsDesignatorContext,
7974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &Index,
7984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     InitListExpr *StructuredList,
7994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &StructuredIndex) {
8000cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  // Check for the special-case of initializing an array with a string.
8010cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
80279e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
80379e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner                                 SemaRef.Context)) {
80479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner      CheckStringInit(Str, DeclType, SemaRef);
8054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // We place the string literal directly into the resulting
8064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // initializer list. This is the only place where the structure
8074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // of the structured initializer list doesn't match exactly,
8084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // because doing so would involve allocating one character
8094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // constant for each string.
810f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner      UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
8110820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
8120cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      ++Index;
8130cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      return;
8140cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
8150cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
816c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const VariableArrayType *VAT =
8170820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsVariableArrayType(DeclType)) {
818638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // Check for VLAs; in standard C it would be possible to check this
819638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // earlier, but I don't know where clang accepts VLAs (gcc accepts
820638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // them in all sorts of strange places).
8210820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
822dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_variable_object_no_init)
823dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << VAT->getSizeExpr()->getSourceRange();
824638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    hadError = true;
8254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
8264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
827638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
828638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  }
829638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman
83005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // We might know the maximum number of elements in advance.
8314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  llvm::APSInt maxElements(elementIndex.getBitWidth(),
8324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           elementIndex.isUnsigned());
83305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool maxElementsKnown = false;
83405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (const ConstantArrayType *CAT =
8350820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
83605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElements = CAT->getSize();
837f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor    elementIndex.extOrTrunc(maxElements.getBitWidth());
838e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    elementIndex.setIsUnsigned(maxElements.isUnsigned());
83905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElementsKnown = true;
84005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
84105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
8420820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
843c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                             ->getElementType();
84405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
84505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
84605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
84787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
84887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
84987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
85087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
85187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
85287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
85387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. elementIndex will be
85487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // updated to be the next array element we'll initialize.
85587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
8564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, 0, &elementIndex, Index,
857eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex, true,
858eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     false)) {
85905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
86087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        continue;
86187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      }
86287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
863f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      if (elementIndex.getBitWidth() > maxElements.getBitWidth())
864f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor        maxElements.extend(elementIndex.getBitWidth());
865f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
866f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor        elementIndex.extend(maxElements.getBitWidth());
867e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor      elementIndex.setIsUnsigned(maxElements.isUnsigned());
868f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor
86987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If the array is of incomplete type, keep track of the number of
87087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // elements in the initializer.
87187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!maxElementsKnown && elementIndex > maxElements)
87287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        maxElements = elementIndex;
87305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
87405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
87505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
87605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
87705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we know the maximum number of elements, and we've already
87805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // hit it, stop consuming elements in the initializer list.
87905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElementsKnown && elementIndex == maxElements)
8800cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      break;
88105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
88205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // Check this element.
8836fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, elementType, Index,
8844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
88505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++elementIndex;
88605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
88705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If the array is of incomplete type, keep track of the number of
88805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // elements in the initializer.
88905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (!maxElementsKnown && elementIndex > maxElements)
89005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      maxElements = elementIndex;
8910cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
8920cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (DeclType->isIncompleteArrayType()) {
8930cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // If this is an incomplete array type, the actual type needs to
894396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar    // be calculated here.
895e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
89605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElements == Zero) {
897396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // Sizing an array implicitly to zero is not allowed by ISO C,
898396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // but is supported by GNU.
8990820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
900396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                    diag::ext_typecheck_zero_array_size);
9010cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
902396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar
9030820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
904396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                                                     ArrayType::Normal, 0);
9050cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
9060cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
9070cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
9080cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
9090cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff                                            QualType DeclType,
91087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            RecordDecl::field_iterator Field,
91187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            bool SubobjectIsDesignatorContext,
9124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
9134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
914eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
915eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
916b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
9170cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
918b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // If the record is invalid, some of it's members are invalid. To avoid
919b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // confusion, we forgo checking the intializer for the entire record.
920b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  if (structDecl->isInvalidDecl()) {
921b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    hadError = true;
922b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    return;
923b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  }
9243498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
9253498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  if (DeclType->isUnionType() && IList->getNumInits() == 0) {
9263498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    // Value-initialize the first named member of the union.
9273498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
9283498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    for (RecordDecl::field_iterator FieldEnd = RD->field_end();
9293498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor         Field != FieldEnd; ++Field) {
9303498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      if (Field->getDeclName()) {
9313498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor        StructuredList->setInitializedFieldInUnion(*Field);
9323498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor        break;
9333498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      }
9343498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    }
9353498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return;
9363498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  }
9373498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
93805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // If structDecl is a forward declaration, this loop won't do
93905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // anything except look at designated initializers; That's okay,
94005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // because an error should get printed out elsewhere. It might be
94105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // worthwhile to skip over the rest of the initializer, though.
94244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
94387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  RecordDecl::field_iterator FieldEnd = RD->field_end();
944dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor  bool InitializedSomething = false;
94505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
94605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
94705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
94805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
94987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
95087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
95187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
95287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
95387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
95487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
95587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. Field will be updated to
95687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the next field that we'll be initializing.
95787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
9584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, &Field, 0, Index,
959eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex,
960eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, TopLevelObject))
96105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
96205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
963dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      InitializedSomething = true;
96405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
96505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
96605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
96705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field == FieldEnd) {
96805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      // We've run out of fields. We're done.
96944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      break;
97005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
97144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
972dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    // We've already initialized a member of a union. We're done.
973dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    if (InitializedSomething && DeclType->isUnionType())
974dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      break;
975dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor
97605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we've hit the flexible array member at the end, we're done.
97705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field->getType()->isIncompleteArrayType())
978b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      break;
97944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
9800bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (Field->isUnnamedBitfield()) {
9814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // Don't initialize unnamed bitfields, e.g. "int : 20;"
98205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Field;
983b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      continue;
9840cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
98544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
9866fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, Field->getType(), Index,
9874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
988dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    InitializedSomething = true;
9890bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
9900bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (DeclType->isUnionType()) {
9910bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Initialize the first field within the union.
9920bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      StructuredList->setInitializedFieldInUnion(*Field);
9930bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
99405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
99505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++Field;
9960cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
99744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
998eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
999a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      Index >= IList->getNumInits())
1000eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    return;
1001eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1002eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  // Handle GNU flexible array initializers.
1003eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  if (!TopLevelObject &&
1004a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      (!isa<InitListExpr>(IList->getInit(Index)) ||
1005a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor       cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
10060820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1007eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                  diag::err_flexible_array_init_nonempty)
1008eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      << IList->getInit(Index)->getSourceRange().getBegin();
10090820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1010eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      << *Field;
1011eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    hadError = true;
1012a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    ++Index;
1013a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    return;
1014a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  } else {
1015a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1016a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                 diag::ext_flexible_array_init)
1017a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      << IList->getInit(Index)->getSourceRange().getBegin();
1018a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1019a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      << *Field;
1020eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  }
1021eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1022a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  if (isa<InitListExpr>(IList->getInit(Index)))
1023a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1024a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                        StructuredIndex);
1025a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  else
1026a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1027a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                          StructuredIndex);
10280cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
10290cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
103005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @brief Check the well-formedness of a C99 designated initializer.
103105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
103205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Determines whether the designated initializer @p DIE, which
103305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// resides at the given @p Index within the initializer list @p
103405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// IList, is well-formed for a current object of type @p DeclType
103505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// (C99 6.7.8). The actual subobject that this designator refers to
103605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// within the current subobject is returned in either
10374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @p NextField or @p NextElementIndex (whichever is appropriate).
103805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
103905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param IList  The initializer list in which this designated
104005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// initializer occurs.
104105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
104205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param DIE  The designated initializer and its initialization
104305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// expression.
104405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
104505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param DeclType  The type of the "current object" (C99 6.7.8p17),
104605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// into which the designation in @p DIE should refer.
104705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
104887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextField  If non-NULL and the first designator in @p DIE is
104987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// a field, this will be set to the field declaration corresponding
105087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// to the field named by the designator.
105105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
105287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextElementIndex  If non-NULL and the first designator in @p
105387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// DIE is an array designator or GNU array-range designator, this
105487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// will be set to the last index initialized by this designator.
105505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
105605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param Index  Index into @p IList where the designated initializer
105705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @p DIE occurs.
105805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
10594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @param StructuredList  The initializer list expression that
10604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// describes all of the subobject initializers in the order they'll
10614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// actually be initialized.
10624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor///
106305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @returns true if there was an error, false otherwise.
106487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregorbool
106587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas GregorInitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
106687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      DesignatedInitExpr *DIE,
106719da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner                                     DesignatedInitExpr::designators_iterator D,
106887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      QualType &CurrentObjectType,
106987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      RecordDecl::field_iterator *NextField,
107087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      llvm::APSInt *NextElementIndex,
10714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
10724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
107334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                      unsigned &StructuredIndex,
1074eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool FinishSubobjectInit,
1075eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
107687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (D == DIE->designators_end()) {
107787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the actual initialization for the designated object type.
107887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
10796fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
10806fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Temporarily remove the designator expression from the
10816fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // initializer list that the child calls see, so that we don't try
10826fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // to re-process the designator.
10836fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    unsigned OldIndex = Index;
10846fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE->getInit());
10856fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
10866fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, CurrentObjectType, Index,
10874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
10886fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
10896fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Restore the designated initializer expression in the syntactic
10906fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // form of the initializer list.
10916fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    if (IList->getInit(OldIndex) != DIE->getInit())
10926fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      DIE->setInit(IList->getInit(OldIndex));
10936fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE);
10946fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
109587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
109687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
109705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
10984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  bool IsFirstDesignator = (D == DIE->designators_begin());
10994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  assert((IsFirstDesignator || StructuredList) &&
11004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor         "Need a non-designated initializer list to start from");
11014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
11024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Determine the structural initializer list that corresponds to the
11034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // current subobject.
11044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
1105ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
1106ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                                 StructuredList, StructuredIndex,
11074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                 SourceRange(D->getStartLocation(),
11084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                             DIE->getSourceRange().getEnd()));
11094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  assert(StructuredList && "Expected a structured initializer list");
11104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
111187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (D->isFieldDesignator()) {
111287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // C99 6.7.8p7:
111387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
111487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   If a designator has the form
111587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
111687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //      . identifier
111787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
111887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   then the current object (defined below) shall have
111987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   structure or union type and the identifier shall be the
112087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   name of a member of that type.
112187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    const RecordType *RT = CurrentObjectType->getAsRecordType();
112287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (!RT) {
112387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      SourceLocation Loc = D->getDotLoc();
112487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (Loc.isInvalid())
112587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        Loc = D->getFieldLoc();
11260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
11270820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
112887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
112987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
113087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
113187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
11324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Note: we perform a linear search of the fields here, despite
11334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the fact that we have a faster lookup method, because we always
11344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // need to compute the field's index.
113587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IdentifierInfo *FieldName = D->getFieldName();
11364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned FieldIndex = 0;
11374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    RecordDecl::field_iterator Field = RT->getDecl()->field_begin(),
11384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                            FieldEnd = RT->getDecl()->field_end();
11394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    for (; Field != FieldEnd; ++Field) {
11404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->isUnnamedBitfield())
11414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        continue;
11424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
11434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->getIdentifier() == FieldName)
11444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        break;
11454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
11464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++FieldIndex;
114787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
114887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
11494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (Field == FieldEnd) {
11504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // We did not find the field we're looking for. Produce a
11514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // suitable diagnostic and return a failure.
11524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
11534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Lookup.first == Lookup.second) {
11544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        // Name lookup didn't find anything.
11550820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
11564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor          << FieldName << CurrentObjectType;
11574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      } else {
11584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        // Name lookup found something, but it wasn't a field.
11590820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
11604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor          << FieldName;
11610820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag((*Lookup.first)->getLocation(),
11624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                      diag::note_field_designator_found);
11634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      }
11644c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
11654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++Index;
11664c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      return true;
11674c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    } else if (cast<RecordDecl>((*Field)->getDeclContext())
11684c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 ->isAnonymousStructOrUnion()) {
11690820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_anon_class)
11704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        << FieldName
11714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        << (cast<RecordDecl>((*Field)->getDeclContext())->isUnion()? 2 :
11720820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner            (int)SemaRef.getLangOptions().CPlusPlus);
11730820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag((*Field)->getLocation(), diag::note_field_designator_found);
117487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
117587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
117687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
11774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
11784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // All of the fields of a union are located at the same place in
11794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the initializer list.
11800bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (RT->getDecl()->isUnion()) {
11814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      FieldIndex = 0;
11820bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      StructuredList->setInitializedFieldInUnion(*Field);
11830bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
11844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
118587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Update the designator with the field declaration.
11864c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    D->setField(*Field);
118705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
11884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Make sure that our non-designated initializer list has space
11894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // for a subobject corresponding to this field.
11904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (FieldIndex >= StructuredList->getNumInits())
11910820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
11924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1193eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    // This designator names a flexible array member.
1194eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    if (Field->getType()->isIncompleteArrayType()) {
1195eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool Invalid = false;
1196eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      DesignatedInitExpr::designators_iterator NextD = D;
1197eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      ++NextD;
1198eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (NextD != DIE->designators_end()) {
1199eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // We can't designate an object within the flexible array
1200eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // member (because GCC doesn't allow it).
12010820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(NextD->getStartLocation(),
1202eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_designator_into_flexible_array_member)
1203eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << SourceRange(NextD->getStartLocation(),
1204eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                         DIE->getSourceRange().getEnd());
12050820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1206eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1207eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1208eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1209eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1210eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1211eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // The initializer is not an initializer list.
12120820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
1213eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_flexible_array_init_needs_braces)
1214eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << DIE->getInit()->getSourceRange();
12150820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1216eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1217eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1218eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1219eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1220eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Handle GNU flexible array initializers.
1221eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (!Invalid && !TopLevelObject &&
1222eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
12230820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(DIE->getSourceRange().getBegin(),
1224eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_flexible_array_init_nonempty)
1225eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << DIE->getSourceRange().getBegin();
12260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1227eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1228eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1229eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1230eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1231eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (Invalid) {
1232eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Index;
1233eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1234eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1235eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1236eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Initialize the array.
1237eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool prevHadError = hadError;
1238eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1239eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned OldIndex = Index;
1240eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(Index, DIE->getInit());
1241eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      CheckSubElementType(IList, Field->getType(), Index,
1242eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                          StructuredList, newStructuredIndex);
1243eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(OldIndex, DIE);
1244eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (hadError && !prevHadError) {
1245eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Field;
1246eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++FieldIndex;
1247eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        if (NextField)
1248eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          *NextField = Field;
1249eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        StructuredIndex = FieldIndex;
1250eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1251eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1252eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    } else {
1253eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Recurse to check later designated subobjects.
1254eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType FieldType = (*Field)->getType();
1255eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1256eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index,
1257eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, newStructuredIndex,
1258eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, false))
1259eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1260eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    }
126187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
126287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Find the position of the next field to be initialized in this
126387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // subobject.
126487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Field;
12654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++FieldIndex;
126605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
126787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // If this the first designator, our caller will continue checking
126887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // the rest of this struct/class/union subobject.
126987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (IsFirstDesignator) {
127087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (NextField)
127187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        *NextField = Field;
12724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      StructuredIndex = FieldIndex;
127387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return false;
127487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
127505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
127634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (!FinishSubobjectInit)
127734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return false;
127834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
127987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the remaining fields within this class/struct/union subobject.
128087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
12814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
12824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          StructuredList, FieldIndex);
128387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
128487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
128505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
128687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // C99 6.7.8p6:
128787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
128887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   If a designator has the form
128987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
129087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ]
129187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
129287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   then the current object (defined below) shall have array
129387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   type and the expression shall be an integer constant
129487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   expression. If the array is of unknown size, any
129587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   nonnegative value is valid.
129687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
129787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Additionally, cope with the GNU extension that permits
129887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // designators of the form
129987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
130087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ... constant-expression ]
13010820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
130287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (!AT) {
13030820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
130487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      << CurrentObjectType;
130587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Index;
130687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return true;
130787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
130805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
130987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  Expr *IndexExpr = 0;
131034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
131134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (D->isArrayDesignator()) {
131287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayIndex(*D);
131334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
131434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    bool ConstExpr
13150820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      = IndexExpr->isIntegerConstantExpr(DesignatedStartIndex, SemaRef.Context);
131634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    assert(ConstExpr && "Expression must be constant"); (void)ConstExpr;
131734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
131834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex = DesignatedStartIndex;
131934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
132087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    assert(D->isArrayRangeDesignator() && "Need array-range designator");
132134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
132234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    bool StartConstExpr
132334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      = DIE->getArrayRangeStart(*D)->isIntegerConstantExpr(DesignatedStartIndex,
13240820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                                                           SemaRef.Context);
132534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    assert(StartConstExpr && "Expression must be constant"); (void)StartConstExpr;
132634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
132734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    bool EndConstExpr
132834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      = DIE->getArrayRangeEnd(*D)->isIntegerConstantExpr(DesignatedEndIndex,
13290820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                                                         SemaRef.Context);
133034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    assert(EndConstExpr && "Expression must be constant"); (void)EndConstExpr;
133134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
133287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayRangeEnd(*D);
133334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
133434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedStartIndex.getZExtValue() != DesignatedEndIndex.getZExtValue())
1335a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor      FullyStructuredList->sawArrayRangeDesignator();
133687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
133705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
133887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (isa<ConstantArrayType>(AT)) {
133987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
134034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
134134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
134234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
134334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
134434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedEndIndex >= MaxElements) {
13450820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
134687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                    diag::err_array_designator_too_large)
134734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor        << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
134887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        << IndexExpr->getSourceRange();
134987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
135087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
135105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
135234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
135334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Make sure the bit-widths and signedness match.
135434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
135534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
135634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    else if (DesignatedStartIndex.getBitWidth() < DesignatedEndIndex.getBitWidth())
135734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
135834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(true);
135934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(true);
136005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
136187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
13624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Make sure that our non-designated initializer list has space
13634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // for a subobject corresponding to this array element.
136434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
13650820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    StructuredList->resizeInits(SemaRef.Context,
136634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                DesignatedEndIndex.getZExtValue() + 1);
136734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
136834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Repeatedly perform subobject initializations in the range
136934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // [DesignatedStartIndex, DesignatedEndIndex].
137034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
137134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Move to the next designator
137234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
137334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned OldIndex = Index;
137434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  ++D;
137534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  while (DesignatedStartIndex <= DesignatedEndIndex) {
137634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Recurse to check later designated subobjects.
137734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    QualType ElementType = AT->getElementType();
137834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    Index = OldIndex;
137934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (CheckDesignatedInitializer(IList, DIE, D, ElementType, 0, 0, Index,
138034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                   StructuredList, ElementIndex,
1381eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   (DesignatedStartIndex == DesignatedEndIndex),
1382eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   false))
138334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return true;
138487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
138534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Move to the next index in the array that we'll be initializing.
138634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ++DesignatedStartIndex;
138734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ElementIndex = DesignatedStartIndex.getZExtValue();
138834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  }
138987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
139087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // If this the first designator, our caller will continue checking
139187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // the rest of this array subobject.
139287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (IsFirstDesignator) {
139387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (NextElementIndex)
139434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      *NextElementIndex = DesignatedStartIndex;
13954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredIndex = ElementIndex;
139687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return false;
139787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
139834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
139934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (!FinishSubobjectInit)
140034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    return false;
140134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
140287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Check the remaining elements within this array subobject.
140305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool prevHadError = hadError;
1404fdf556936f94344d5482747403f27822cf0ae37fDouglas Gregor  CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
14054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 StructuredList, ElementIndex);
140687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  return hadError && !prevHadError;
140705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
140805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
14094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// Get the structured initializer list for a subobject of type
14104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// @p CurrentObjectType.
14114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListExpr *
14124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
14134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType CurrentObjectType,
14144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
14154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned StructuredIndex,
14164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            SourceRange InitRange) {
14174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *ExistingInit = 0;
14184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
14194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = SyntacticToSemantic[IList];
14204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else if (StructuredIndex < StructuredList->getNumInits())
14214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = StructuredList->getInit(StructuredIndex);
14224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
14234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
14244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return Result;
14254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
14264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (ExistingInit) {
14274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // We are creating an initializer list that initializes the
14284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobjects of the current object, but there was already an
14294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // initialization that completely initialized the current
14304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject, e.g., by a compound literal:
14314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    //
14324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X { int a, b; };
14334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
14344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    //
14354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
14364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // designated initializer re-initializes the whole
14374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject [0], overwriting previous initializers.
1438ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    SemaRef.Diag(InitRange.getBegin(),
1439ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                 diag::warn_subobject_initializer_overrides)
14404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << InitRange;
14410820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
14424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
144354f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
14444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << ExistingInit->getSourceRange();
14454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
14464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
14474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitListExpr *Result
1448ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
1449ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                                         InitRange.getEnd());
1450ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor
14514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Result->setType(CurrentObjectType);
14524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1453fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  // Pre-allocate storage for the structured initializer list.
1454fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  unsigned NumElements = 0;
145508457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  unsigned NumInits = 0;
145608457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  if (!StructuredList)
145708457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    NumInits = IList->getNumInits();
145808457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  else if (Index < IList->getNumInits()) {
145908457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
146008457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      NumInits = SubList->getNumInits();
146108457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  }
146208457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor
1463fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  if (const ArrayType *AType
1464fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1465fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1466fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = CAType->getSize().getZExtValue();
1467fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // Simple heuristic so that we don't allocate a very large
1468fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // initializer with many empty entries at the end.
146908457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      if (NumInits && NumElements > NumInits)
1470fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor        NumElements = 0;
1471fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    }
1472fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  } else if (const VectorType *VType = CurrentObjectType->getAsVectorType())
1473fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = VType->getNumElements();
1474fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  else if (const RecordType *RType = CurrentObjectType->getAsRecordType()) {
1475fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    RecordDecl *RDecl = RType->getDecl();
1476fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (RDecl->isUnion())
1477fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = 1;
1478fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    else
1479fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = std::distance(RDecl->field_begin(), RDecl->field_end());
1480fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  }
1481fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
148208457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  if (NumElements < NumInits)
1483fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = IList->getNumInits();
1484fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
1485fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  Result->reserveInits(NumElements);
1486fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
14874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Link this new initializer list into the structured initializer
14884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // lists.
14894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (StructuredList)
14904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredList->updateInit(StructuredIndex, Result);
14914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else {
14924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    Result->setSyntacticForm(IList);
14934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    SyntacticToSemantic[IList] = Result;
14944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
14954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
14964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
14974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
14984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
14994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Update the initializer at index @p StructuredIndex within the
15004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// structured initializer list to the value @p expr.
15014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
15024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  unsigned &StructuredIndex,
15034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  Expr *expr) {
15044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // No structured initializer list to update
15054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
15064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
15074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
15094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // This initializer overwrites a previous initializer. Warn.
15100820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(expr->getSourceRange().getBegin(),
15114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::warn_initializer_overrides)
15124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << expr->getSourceRange();
15130820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
15144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
151554f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
15164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << PrevInit->getSourceRange();
15174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
15184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  ++StructuredIndex;
15204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
15214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
152205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Check that the given Index expression is a valid array designator
152305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// value. This is essentailly just a wrapper around
152405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Expr::isIntegerConstantExpr that also checks for negative values
152505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// and produces a reasonable diagnostic if there is a
152605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// failure. Returns true if there was an error, false otherwise.  If
152705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// everything went okay, Value will receive the value of the constant
152805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// expression.
152905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregorstatic bool
153005c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorCheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) {
153105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation Loc = Index->getSourceRange().getBegin();
153205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
153305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Make sure this is an integer constant expression.
153405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc))
153505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return Self.Diag(Loc, diag::err_array_designator_nonconstant)
153605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      << Index->getSourceRange();
153705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
153805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Make sure this constant expression is non-negative.
1539e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor  llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()),
1540e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor                    Value.isUnsigned());
154105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Value < Zero)
154205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return Self.Diag(Loc, diag::err_array_designator_negative)
154305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      << Value.toString(10) << Index->getSourceRange();
154405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
154553d3d8e0662197f7245d8f5ff697a72a2b4b3f54Douglas Gregor  Value.setIsUnsigned(true);
154605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return false;
154705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
154805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
154905c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
155005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                                        SourceLocation Loc,
1551eeae8f072748affce25ab4064982626361293390Douglas Gregor                                                        bool GNUSyntax,
155205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                                        OwningExprResult Init) {
155305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  typedef DesignatedInitExpr::Designator ASTDesignator;
155405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
155505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool Invalid = false;
155605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  llvm::SmallVector<ASTDesignator, 32> Designators;
155705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  llvm::SmallVector<Expr *, 32> InitExpressions;
155805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
155905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Build designators and check array designator expressions.
156005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
156105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    const Designator &D = Desig.getDesignator(Idx);
156205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    switch (D.getKind()) {
156305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::FieldDesignator:
156405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
156505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                          D.getFieldLoc()));
156605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
156705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
156805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayDesignator: {
156905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *Index = static_cast<Expr *>(D.getArrayIndex());
157005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt IndexValue;
157105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      if (CheckArrayDesignatorExpr(*this, Index, IndexValue))
157205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
157305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      else {
157405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Designators.push_back(ASTDesignator(InitExpressions.size(),
157505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                            D.getLBracketLoc(),
157605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                            D.getRBracketLoc()));
157705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        InitExpressions.push_back(Index);
157805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
157905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
158005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
158105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
158205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayRangeDesignator: {
158305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
158405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
158505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt StartValue;
158605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt EndValue;
158705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) ||
158805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor          CheckArrayDesignatorExpr(*this, EndIndex, EndValue))
158905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
1590d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor      else {
1591d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        // Make sure we're comparing values with the same bit width.
1592d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        if (StartValue.getBitWidth() > EndValue.getBitWidth())
1593d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          EndValue.extend(StartValue.getBitWidth());
1594d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1595d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          StartValue.extend(EndValue.getBitWidth());
1596d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor
1597d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        if (EndValue < StartValue) {
1598d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1599d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor            << StartValue.toString(10) << EndValue.toString(10)
1600d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor            << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1601d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Invalid = true;
1602d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        } else {
1603d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Designators.push_back(ASTDesignator(InitExpressions.size(),
1604d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getLBracketLoc(),
1605d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getEllipsisLoc(),
1606d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getRBracketLoc()));
1607d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(StartIndex);
1608d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(EndIndex);
1609d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        }
161005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
161105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
161205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
161305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
161405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
161505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
161605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Invalid || Init.isInvalid())
161705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return ExprError();
161805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
161905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Clear out the expressions within the designation.
162005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Desig.ClearExprs(*this);
162105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
162205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  DesignatedInitExpr *DIE
162305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(),
162405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                 &InitExpressions[0], InitExpressions.size(),
1625eeae8f072748affce25ab4064982626361293390Douglas Gregor                                 Loc, GNUSyntax,
162605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                 static_cast<Expr *>(Init.release()));
162705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return Owned(DIE);
162805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
1629c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
1630c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorbool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
16310820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  InitListChecker CheckInitList(*this, InitList, DeclType);
1632c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  if (!CheckInitList.HadError())
1633c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor    InitList = CheckInitList.getFullyStructuredList();
1634c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
1635c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  return CheckInitList.HadError();
1636c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor}
163787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
163887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// \brief Diagnose any semantic errors with value-initialization of
163987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// the given type.
164087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor///
164187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// Value-initialization effectively zero-initializes any types
164287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// without user-declared constructors, and calls the default
164387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// constructor for a for any type that has a user-declared
164487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
164587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// a type with a user-declared constructor does not have an
164687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// accessible, non-deleted default constructor. In C, everything can
164787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// be value-initialized, which corresponds to C's notion of
164887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// initializing objects with static storage duration when no
164987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// initializer is provided for that object.
165087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor///
165187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// \returns true if there was an error, false otherwise.
165287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregorbool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
165387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  // C++ [dcl.init]p5:
165487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  //
165587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  //   To value-initialize an object of type T means:
165687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
165787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  //     -- if T is an array type, then each element is value-initialized;
165887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (const ArrayType *AT = Context.getAsArrayType(Type))
165987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    return CheckValueInitialization(AT->getElementType(), Loc);
166087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
166187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (const RecordType *RT = Type->getAsRecordType()) {
1662c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
166387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      // -- if T is a class type (clause 9) with a user-declared
166487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      //    constructor (12.1), then the default constructor for T is
166587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      //    called (and the initialization is ill-formed if T has no
166687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      //    accessible default constructor);
1667c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor      if (ClassDecl->hasUserDeclaredConstructor())
166887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        // FIXME: Eventually, we'll need to put the constructor decl
166987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        // into the AST.
167087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        return PerformInitializationByConstructor(Type, 0, 0, Loc,
167187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor                                                  SourceRange(Loc),
167287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor                                                  DeclarationName(),
167387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor                                                  IK_Direct);
167487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    }
167587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
167687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
167787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (Type->isReferenceType()) {
167887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    // C++ [dcl.init]p5:
167987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    //   [...] A program that calls for default-initialization or
168087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    //   value-initialization of an entity of reference type is
168187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    //   ill-formed. [...]
1682d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor    // FIXME: Once we have code that goes through this path, add an
1683d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor    // actual diagnostic :)
168487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
168587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
168687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  return false;
168787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor}
1688