SemaInit.cpp revision 587cbdfd95f4b0aaccc14b31f5debe85d5daf7ed
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"
212078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson#include "clang/AST/ExprCXX.h"
2279e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner#include "clang/AST/ExprObjC.h"
23c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor#include <map>
2405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregorusing namespace clang;
250cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
26dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
27dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Sema Initialization Checking
28dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
29dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
3079e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattnerstatic Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
318879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  const ArrayType *AT = Context.getAsArrayType(DeclType);
328879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (!AT) return 0;
338879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
348718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
358718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman    return 0;
368718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman
378879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // See if this is a string literal or @encode.
388879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  Init = Init->IgnoreParens();
398879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
408879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Handle @encode, which is a narrow string.
418879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
428879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
438879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
448879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Otherwise we can only handle string literals.
458879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  StringLiteral *SL = dyn_cast<StringLiteral>(Init);
46220b6369d7717bfe6894b46cef055d3e763827f2Chris Lattner  if (SL == 0) return 0;
478879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
488879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // char array can be initialized with a narrow string.
498879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Only allow char x[] = "foo";  not char x[] = L"foo";
508879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (!SL->isWide())
518879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return AT->getElementType()->isCharType() ? Init : 0;
528879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
538879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // wchar_t array can be initialized with a wide string: C99 6.7.8p15:
548879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // "An array with element type compatible with wchar_t may be initialized by a
558879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // wide string literal, optionally enclosed in braces."
5619753cfa6059b237880a91f21ef58f2d8984845fChris Lattner  if (Context.typesAreCompatible(Context.getWCharType(), AT->getElementType()))
578879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    // Only allow wchar_t x[] = L"foo";  not wchar_t x[] = "foo";
588879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
598879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
60dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  return 0;
61dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
62dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
6395e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattnerstatic bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
6495e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner                                   bool DirectInit, Sema &S) {
65dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // Get the type before calling CheckSingleAssignmentConstraints(), since
66dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // it can promote the expression.
67dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  QualType InitType = Init->getType();
68dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
6995e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  if (S.getLangOptions().CPlusPlus) {
70dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // FIXME: I dislike this error message. A lot.
7195e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner    if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit))
7295e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner      return S.Diag(Init->getSourceRange().getBegin(),
7395e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner                    diag::err_typecheck_convert_incompatible)
7495e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner        << DeclType << Init->getType() << "initializing"
7595e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner        << Init->getSourceRange();
76dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return false;
77dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
78dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
7995e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  Sema::AssignConvertType ConvTy =
8095e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner    S.CheckSingleAssignmentConstraints(DeclType, Init);
8195e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
82dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                  InitType, Init, "initializing");
83dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
84dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
8579e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattnerstatic void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
8679e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  // Get the length of the string as parsed.
8779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  uint64_t StrLength =
8879e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
8979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner
90dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
9179e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  const ArrayType *AT = S.Context.getAsArrayType(DeclT);
92dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
93dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // C99 6.7.8p14. We have an array of character type with unknown size
94dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // being initialized to a string literal.
95dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    llvm::APSInt ConstVal(32);
9619da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    ConstVal = StrLength;
97dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // Return a new array type (C99 6.7.8p22).
98f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner    DeclT = S.Context.getConstantArrayType(IAT->getElementType(), ConstVal,
99f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner                                           ArrayType::Normal, 0);
10019da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    return;
101dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
10219da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner
1038718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
1048718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman
1058718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // C99 6.7.8p14. We have an array of character type with known size.  However,
1068718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // the size may be smaller or larger than the string we are initializing.
1078718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // FIXME: Avoid truncation for 64-bit length strings.
1088718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  if (StrLength-1 > CAT->getSize().getZExtValue())
1098718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman    S.Diag(Str->getSourceRange().getBegin(),
1108718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman           diag::warn_initializer_string_for_char_array_too_long)
1118718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman      << Str->getSourceRange();
1128718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman
1138718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // Set the type to the actual size that we are initializing.  If we have
1148718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // something like:
1158718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  //   char x[1] = "foo";
1168718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // then this will set the string literal's type to char[1].
1178718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  Str->setType(DeclT);
118dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
119dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
120dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattnerbool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
121dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                 SourceLocation InitLoc,
122dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                 DeclarationName InitEntity,
1232078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson                                 bool DirectInit, VarDecl *VD) {
1249ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  if (DeclType->isDependentType() ||
1259ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Init->isTypeDependent() || Init->isValueDependent())
126dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return false;
127dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
128dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // C++ [dcl.init.ref]p1:
1297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  //   A variable declared to be a T& or T&&, that is "reference to type T"
130dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  //   (8.3.2), shall be initialized by an object, or function, of
131dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  //   type T or by an object that can be converted into a T.
132dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (DeclType->isReferenceType())
133dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return CheckReferenceInit(Init, DeclType, 0, false, DirectInit);
134dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
135dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // C99 6.7.8p3: The type of the entity to be initialized shall be an array
136dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // of unknown size ("[]") or an object type that is not a variable array type.
137dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
138dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    return Diag(InitLoc,  diag::err_variable_object_no_init)
139dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    << VAT->getSizeExpr()->getSourceRange();
140dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
141dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
142dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (!InitList) {
143dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // FIXME: Handle wide strings
14479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
14579e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner      CheckStringInit(Str, DeclType, *this);
14619da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner      return false;
14719da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    }
148dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
149dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // C++ [dcl.init]p14:
150dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    //   -- If the destination type is a (possibly cv-qualified) class
151dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    //      type:
152dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
153dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      QualType DeclTypeC = Context.getCanonicalType(DeclType);
154dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      QualType InitTypeC = Context.getCanonicalType(Init->getType());
155dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
156dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //   -- If the initialization is direct-initialization, or if it is
157dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      copy-initialization where the cv-unqualified version of the
158dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      source type is the same class as, or a derived class of, the
159dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      class of the destination, constructors are considered.
160dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
161dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner          IsDerivedFrom(InitTypeC, DeclTypeC)) {
162bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson        const CXXRecordDecl *RD =
163bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson          cast<CXXRecordDecl>(DeclType->getAsRecordType()->getDecl());
164bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson
165bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson        // No need to make a CXXConstructExpr if both the ctor and dtor are
166bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson        // trivial.
167bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson        if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor())
168bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson          return false;
169bffed8a98cf5775cd39dc1765abc75c914513012Anders Carlsson
170dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        CXXConstructorDecl *Constructor
171dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        = PerformInitializationByConstructor(DeclType, &Init, 1,
172dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                             InitLoc, Init->getSourceRange(),
173dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                             InitEntity,
174dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner                                             DirectInit? IK_Direct : IK_Copy);
1752078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson        if (!Constructor)
1762078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson          return true;
1772078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson
1782078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson        // FIXME: What do do if VD is null here?
1797c520cf417e7a4eceeefef0a8b2a2b33811f0481Anders Carlsson        if (VD)
1807c520cf417e7a4eceeefef0a8b2a2b33811f0481Anders Carlsson          Init = CXXConstructExpr::Create(Context, VD, DeclType, Constructor,
1817c520cf417e7a4eceeefef0a8b2a2b33811f0481Anders Carlsson                                          false, &Init, 1);
1822078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson        return false;
183dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      }
184dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
185dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //   -- Otherwise (i.e., for the remaining copy-initialization
186dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      cases), user-defined conversion sequences that can
187dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      convert from the source type to the destination type or
188dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      (when a conversion function is used) to a derived class
189dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      thereof are enumerated as described in 13.3.1.4, and the
190dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      best one is chosen through overload resolution
191dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      (13.3). If the conversion cannot be done or is
192dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      ambiguous, the initialization is ill-formed. The
193dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      function selected is called with the initializer
194dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      expression as its argument; if the function is a
195dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      constructor, the call initializes a temporary of the
196dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      //      destination type.
197390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // FIXME: We're pretending to do copy elision here; return to this when we
198390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // have ASTs for such things.
199dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      if (!PerformImplicitConversion(Init, DeclType, "initializing"))
200dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return false;
201dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
202dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      if (InitEntity)
203dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return Diag(InitLoc, diag::err_cannot_initialize_decl)
204dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
205dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << Init->getType() << Init->getSourceRange();
206dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      else
207dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
208dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
209dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner        << Init->getType() << Init->getSourceRange();
210dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    }
211dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
212dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // C99 6.7.8p16.
213dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    if (DeclType->isArrayType())
214dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      return Diag(Init->getLocStart(), diag::err_array_init_list_required)
215dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner      << Init->getSourceRange();
216dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
21795e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner    return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
218dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
219dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
220dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  bool hadError = CheckInitList(InitList, DeclType);
221dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  Init = InitList;
222dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  return hadError;
223dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
224dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
225dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
226dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Semantic checking for initializer lists.
227dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
228dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
2299e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// @brief Semantic checking for initializer lists.
2309e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
2319e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// The InitListChecker class contains a set of routines that each
2329e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// handle the initialization of a certain kind of entity, e.g.,
2339e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arrays, vectors, struct/union types, scalars, etc. The
2349e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// InitListChecker itself performs a recursive walk of the subobject
2359e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure of the type to be initialized, while stepping through
2369e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// the initializer list one element at a time. The IList and Index
2379e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// parameters to each of the Check* routines contain the active
2389e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// (syntactic) initializer list and the index into that initializer
2399e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list that represents the current initializer. Each routine is
2409e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// responsible for moving that Index forward as it consumes elements.
2419e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
2429e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// Each Check* routine also has a StructuredList/StructuredIndex
2439e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arguments, which contains the current the "structured" (semantic)
2449e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initializer list and the index into that initializer list where we
2459e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// are copying initializers as we map them over to the semantic
2469e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list. Once we have completed our recursive walk of the subobject
2479e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure, we will have constructed a full semantic initializer
2489e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list.
2499e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
2509e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// C99 designators cause changes in the initializer list traversal,
2519e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// because they make the initialization "jump" into a specific
2529e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// subobject and then continue the initialization from that
2539e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// point. CheckDesignatedInitializer() recursively steps into the
2549e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// designated subobject and manages backing out the recursion to
2559e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initialize the subobjects after the one designated.
2568b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattnernamespace {
257c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorclass InitListChecker {
2580820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  Sema &SemaRef;
259c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool hadError;
260c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
261c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *FullyStructuredList;
262c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
263c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
2649e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
265eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
266eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
267c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckExplicitInitList(InitListExpr *IList, QualType &T,
2689e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
269eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
270eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
271c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
272c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             bool SubobjectIsDesignatorContext,
273c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             unsigned &Index,
2749e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
275eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
276eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
277c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckSubElementType(InitListExpr *IList, QualType ElemType,
278c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                           unsigned &Index,
2799e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           InitListExpr *StructuredList,
2809e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           unsigned &StructuredIndex);
281930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  void CheckScalarType(InitListExpr *IList, QualType DeclType,
282c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                       unsigned &Index,
2839e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2849e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
285930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  void CheckReferenceType(InitListExpr *IList, QualType DeclType,
286930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &Index,
287930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          InitListExpr *StructuredList,
288930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &StructuredIndex);
289c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
2909e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2919e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
292c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
293c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             RecordDecl::field_iterator Field,
294c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             bool SubobjectIsDesignatorContext, unsigned &Index,
2959e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
296eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
297eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
298c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckArrayType(InitListExpr *IList, QualType &DeclType,
299c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                      llvm::APSInt elementIndex,
300c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                      bool SubobjectIsDesignatorContext, unsigned &Index,
3019e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      InitListExpr *StructuredList,
3029e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      unsigned &StructuredIndex);
303c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
304711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                  unsigned DesigIdx,
305c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  QualType &CurrentObjectType,
306c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  RecordDecl::field_iterator *NextField,
307c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  llvm::APSInt *NextElementIndex,
308c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &Index,
309c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  InitListExpr *StructuredList,
310c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &StructuredIndex,
311eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool FinishSubobjectInit,
312eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool TopLevelObject);
313c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
314c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           QualType CurrentObjectType,
315c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           InitListExpr *StructuredList,
316c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           unsigned StructuredIndex,
317c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           SourceRange InitRange);
3189e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor  void UpdateStructuredListElement(InitListExpr *StructuredList,
3199e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                                   unsigned &StructuredIndex,
320c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                   Expr *expr);
321c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numArrayElements(QualType DeclType);
322c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numStructUnionElements(QualType DeclType);
323930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
324930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  void FillInValueInitializations(InitListExpr *ILE);
325c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorpublic:
3260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
327c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool HadError() { return hadError; }
328c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
329c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // @brief Retrieves the fully-structured initializer list used for
330c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // semantic analysis and code generation.
331c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
332c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor};
3338b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattner} // end anonymous namespace
33468355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
3354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Recursively replaces NULL values within the given initializer list
3364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// with expressions that perform value-initialization of the
3374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// appropriate type.
338930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
3390820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  assert((ILE->getType() != SemaRef.Context.VoidTy) &&
340930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor         "Should not have void type");
34187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  SourceLocation Loc = ILE->getSourceRange().getBegin();
34287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (ILE->getSyntacticForm())
34387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
34487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
3454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
3464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned Init = 0, NumInits = ILE->getNumInits();
3476ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    for (RecordDecl::field_iterator
3486ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor           Field = RType->getDecl()->field_begin(SemaRef.Context),
3496ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor           FieldEnd = RType->getDecl()->field_end(SemaRef.Context);
3504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor         Field != FieldEnd; ++Field) {
3514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->isUnnamedBitfield())
3524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        continue;
3534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
35487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      if (Init >= NumInits || !ILE->getInit(Init)) {
355930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        if (Field->getType()->isReferenceType()) {
356930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          // C++ [dcl.init.aggr]p9:
357930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          //   If an incomplete or empty initializer-list leaves a
358930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          //   member of reference type uninitialized, the program is
359930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          //   ill-formed.
3600820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner          SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
361930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor            << Field->getType()
362930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor            << ILE->getSyntacticForm()->getSourceRange();
3630820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner          SemaRef.Diag(Field->getLocation(),
364930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                        diag::note_uninit_reference_member);
365930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          hadError = true;
36687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          return;
3670820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
36887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          hadError = true;
36987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          return;
370930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        }
37187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
372390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // FIXME: If value-initialization involves calling a constructor, should
373390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we make that call explicit in the representation (even when it means
374390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // extending the initializer list)?
37587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        if (Init < NumInits && !hadError)
37687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          ILE->setInit(Init,
3770820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner              new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
37887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      } else if (InitListExpr *InnerILE
3793498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor                 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
380930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        FillInValueInitializations(InnerILE);
3814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++Init;
382930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
383930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Only look at the first initialization of a union.
384930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      if (RType->getDecl()->isUnion())
385930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        break;
3864c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    }
3874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
3894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
3904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  QualType ElementType;
3924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
39387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumInits = ILE->getNumInits();
39487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumElements = NumInits;
3950820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
3964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = AType->getElementType();
39787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
39887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      NumElements = CAType->getSize().getZExtValue();
39987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
4004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = VType->getElementType();
40187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    NumElements = VType->getNumElements();
40287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  } else
4034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = ILE->getType();
4044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
40587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  for (unsigned Init = 0; Init != NumElements; ++Init) {
40687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (Init >= NumInits || !ILE->getInit(Init)) {
4070820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
40887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        hadError = true;
40987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        return;
41087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      }
41187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
412390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // FIXME: If value-initialization involves calling a constructor, should
413390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // we make that call explicit in the representation (even when it means
414390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // extending the initializer list)?
41587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      if (Init < NumInits && !hadError)
41687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        ILE->setInit(Init,
4170820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                     new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
41887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    }
41968355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner    else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init)))
420930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      FillInValueInitializations(InnerILE);
4214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
4224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
4234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
42468355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
4250820254f97bb8925d933a3664ea1c6fca3997b97Chris LattnerInitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
4260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  : SemaRef(S) {
4270cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  hadError = false;
428c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
429b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  unsigned newIndex = 0;
4304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned newStructuredIndex = 0;
4314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  FullyStructuredList
432ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
433eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
434eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        /*TopLevelObject=*/true);
435c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
436930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  if (!hadError)
437930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    FillInValueInitializations(FullyStructuredList);
4380cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4390cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4400cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numArrayElements(QualType DeclType) {
441638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  // FIXME: use a proper constant
442638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  int maxElements = 0x7FFFFFFF;
443c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT =
4440820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
4450cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = static_cast<int>(CAT->getSize().getZExtValue());
4460cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
4470cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  return maxElements;
4480cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4490cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4500cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numStructUnionElements(QualType DeclType) {
4510cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
4524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int InitializableMembers = 0;
4536ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  for (RecordDecl::field_iterator
4546ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor         Field = structDecl->field_begin(SemaRef.Context),
4556ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor         FieldEnd = structDecl->field_end(SemaRef.Context);
4564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor       Field != FieldEnd; ++Field) {
4574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if ((*Field)->getIdentifier() || !(*Field)->isBitField())
4584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++InitializableMembers;
4594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
46039ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis  if (structDecl->isUnion())
461f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman    return std::min(InitializableMembers, 1);
462f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman  return InitializableMembers - structDecl->hasFlexibleArrayMember();
4630cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4640cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4650cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
4664c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType T, unsigned &Index,
4674c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
468eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
469eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
4700cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  int maxElements = 0;
4710cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4720cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (T->isArrayType())
4730cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numArrayElements(T);
4740cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else if (T->isStructureType() || T->isUnionType())
4750cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numStructUnionElements(T);
476b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  else if (T->isVectorType())
477b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    maxElements = T->getAsVectorType()->getNumElements();
4780cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else
4790cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    assert(0 && "CheckImplicitInitList(): Illegal type");
480b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
481402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  if (maxElements == 0) {
4820820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
483402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman                  diag::err_implicit_empty_initializer);
4844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
485402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    hadError = true;
486402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    return;
487402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  }
488402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman
4894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Build a structured initializer list corresponding to this subobject.
4904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitListExpr *StructuredSubobjectInitList
4914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
4924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                 StructuredIndex,
493ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor          SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
494ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                      ParentIList->getSourceRange().getEnd()));
4954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned StructuredSubobjectInitIndex = 0;
4964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
4974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Check the element types and build the structural subobject.
49887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned StartIndex = Index;
4994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  CheckListElementTypes(ParentIList, T, false, Index,
5004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredSubobjectInitList,
501eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        StructuredSubobjectInitIndex,
502eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        TopLevelObject);
50387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
504a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  StructuredSubobjectInitList->setType(T);
505a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor
506ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor  // Update the structured sub-object initializer so that it's ending
50787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  // range corresponds with the end of the last initializer it used.
50887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (EndIndex < ParentIList->getNumInits()) {
50987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    SourceLocation EndLoc
51087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
51187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    StructuredSubobjectInitList->setRBraceLoc(EndLoc);
51287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
5130cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5140cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
515a647caad2dec67ac25b763f06237cfe3c3968b51Steve Naroffvoid InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
5164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
5174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
518eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
519eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
520c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
5214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  SyntacticToSemantic[IList] = StructuredList;
5224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList->setSyntacticForm(IList);
5234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  CheckListElementTypes(IList, T, true, Index, StructuredList,
524eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        StructuredIndex, TopLevelObject);
525a647caad2dec67ac25b763f06237cfe3c3968b51Steve Naroff  IList->setType(T);
5264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList->setType(T);
527638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (hadError)
528638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
529c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
530638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (Index < IList->getNumInits()) {
531c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    // We have leftover initializers
532c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    if (IList->getNumInits() > 0 &&
5330820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        IsStringInit(IList->getInit(Index), T, SemaRef.Context)) {
5347c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
5350820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.getLangOptions().CPlusPlus)
5367c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor        DK = diag::err_excess_initializers_in_char_array_initializer;
537bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      // Special-case
5380820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
539dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getInit(Index)->getSourceRange();
540c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman      hadError = true;
541d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    } else if (!T->isIncompleteType()) {
542b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // Don't complain for incomplete types, since we'll get an error
543b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // elsewhere
544eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType CurrentObjectType = StructuredList->getType();
545eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      int initKind =
546eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isArrayType()? 0 :
547eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isVectorType()? 1 :
548eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isScalarType()? 2 :
549eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isUnionType()? 3 :
550eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        4;
5517c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
5527c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers;
5530820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      if (SemaRef.getLangOptions().CPlusPlus)
5547c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor          DK = diag::err_excess_initializers;
5557c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
5560820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
557eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        << initKind << IList->getInit(Index)->getSourceRange();
558c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    }
559c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  }
560cda25a977e4b7fe4e080b87586410eaeab7b62f6Eli Friedman
561759f25237864f3a3cc23eb01f0c0ce6edcc9342dEli Friedman  if (T->isScalarType() && !TopLevelObject)
5620820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
563a3a835149ed4b183e3b009a1f94a6123779d696bDouglas Gregor      << IList->getSourceRange()
564a3a835149ed4b183e3b009a1f94a6123779d696bDouglas Gregor      << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart()))
565a3a835149ed4b183e3b009a1f94a6123779d696bDouglas Gregor      << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd()));
5660cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5670cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
568b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedmanvoid InitListChecker::CheckListElementTypes(InitListExpr *IList,
569b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman                                            QualType &DeclType,
57087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            bool SubobjectIsDesignatorContext,
5714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
5724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
573eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
574eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
575c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (DeclType->isScalarType()) {
5766fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
577c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (DeclType->isVectorType()) {
5784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
579d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor  } else if (DeclType->isAggregateType()) {
580d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor    if (DeclType->isRecordType()) {
58187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
5826ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      CheckStructUnionTypes(IList, DeclType, RD->field_begin(SemaRef.Context),
5834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                            SubobjectIsDesignatorContext, Index,
584eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                            StructuredList, StructuredIndex,
585eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                            TopLevelObject);
58687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    } else if (DeclType->isArrayType()) {
587f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      llvm::APSInt Zero(
5880820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                      SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
589f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor                      false);
5904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
5914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                     StructuredList, StructuredIndex);
59287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
5930cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    else
5944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      assert(0 && "Aggregate that isn't a structure or array?!");
595613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff  } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
596613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff    // This type is invalid, issue a diagnostic.
5974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
5980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
599d162584991885ab004a02573a73ce06422b921fcChris Lattner      << DeclType;
600d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    hadError = true;
601930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isRecordType()) {
602930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init]p14:
603930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   [...] If the class is an aggregate (8.5.1), and the initializer
604930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   is a brace-enclosed list, see 8.5.1.
605930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //
606930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Note: 8.5.1 is handled below; here, we diagnose the case where
607930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // we have an initializer list and a destination type that is not
608930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // an aggregate.
609930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // FIXME: In C++0x, this is yet another form of initialization.
6100820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
611930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << DeclType << IList->getSourceRange();
612930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
613930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isReferenceType()) {
614930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
6150cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  } else {
6160cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // In C, all types are either scalars or aggregates, but
6170cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // additional handling is needed here for C++ (and possibly others?).
6180cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    assert(0 && "Unsupported initializer type");
6190cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
6200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
6210cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
622b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedmanvoid InitListChecker::CheckSubElementType(InitListExpr *IList,
623b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman                                          QualType ElemType,
6244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &Index,
6254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          InitListExpr *StructuredList,
6264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &StructuredIndex) {
6276fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor  Expr *expr = IList->getInit(Index);
628c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
629c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    unsigned newIndex = 0;
6304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned newStructuredIndex = 0;
6314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    InitListExpr *newStructuredList
6324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      = getStructuredSubobjectInit(IList, Index, ElemType,
6334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                   StructuredList, StructuredIndex,
6344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                   SubInitList->getSourceRange());
6354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckExplicitInitList(SubInitList, ElemType, newIndex,
6364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          newStructuredList, newStructuredIndex);
6374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
6384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
63979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
64079e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    CheckStringInit(Str, ElemType, SemaRef);
641f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner    UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
6424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
643c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (ElemType->isScalarType()) {
6446fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
645930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (ElemType->isReferenceType()) {
646930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
647b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  } else {
6480820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (SemaRef.getLangOptions().CPlusPlus) {
649930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // C++ [dcl.init.aggr]p12:
650930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   All implicit type conversions (clause 4) are considered when
651930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   initializing the aggregate member with an ini- tializer from
652930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   an initializer-list. If the initializer can initialize a
653930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   member, the member is initialized. [...]
654930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ImplicitConversionSequence ICS
6550820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        = SemaRef.TryCopyInitialization(expr, ElemType);
656930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
6570820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
658930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                               "initializing"))
659930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          hadError = true;
660930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
661930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        ++Index;
662930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        return;
663930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      }
664930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
665930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Fall through for subaggregate initialization
666930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    } else {
667930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // C99 6.7.8p13:
668930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //
669930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   The initializer for a structure or union object that has
670930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   automatic storage duration shall be either an initializer
671930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   list as described below, or a single expression that has
672930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   compatible structure or union type. In the latter case, the
673930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   initial value of the object, including unnamed members, is
674930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   that of the expression.
6758718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman      if (ElemType->isRecordType() &&
6768718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman          SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
677930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
678930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        ++Index;
679930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        return;
680930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      }
681930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
682930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Fall through for subaggregate initialization
683930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
684930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
685930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init.aggr]p12:
686930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //
687930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   [...] Otherwise, if the member is itself a non-empty
688930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   subaggregate, brace elision is assumed and the initializer is
689930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   considered for the initialization of the first member of
690930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   the subaggregate.
691930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (ElemType->isAggregateType() || ElemType->isVectorType()) {
692930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      CheckImplicitInitList(IList, ElemType, Index, StructuredList,
693930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                            StructuredIndex);
694930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
695930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    } else {
696930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // We cannot initialize this element, so let
697930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // PerformCopyInitialization produce the appropriate diagnostic.
6980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
699930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
700930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
701930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
702930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
703930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
704b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman}
705b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
706930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
7076fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor                                      unsigned &Index,
7084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
7094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
7100cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
7116fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    Expr *expr = IList->getInit(Index);
712c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    if (isa<InitListExpr>(expr)) {
7130820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
714dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                    diag::err_many_braces_around_scalar_init)
715dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getSourceRange();
716bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      hadError = true;
717bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      ++Index;
7184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
719bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      return;
72005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    } else if (isa<DesignatedInitExpr>(expr)) {
7210820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(expr->getSourceRange().getBegin(),
72205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                    diag::err_designator_for_scalar_init)
72305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        << DeclType << expr->getSourceRange();
72405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      hadError = true;
72505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Index;
7264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
72705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      return;
7280cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
72905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
730c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
7310820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
732bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      hadError = true; // types weren't compatible.
73305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else if (savExpr != expr) {
734c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman      // The type was promoted, update initializer list.
7356fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      IList->setInit(Index, expr);
73605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
7374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (hadError)
7384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
7394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    else
7404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
7410cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    ++Index;
742bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman  } else {
7430820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
744dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << IList->getSourceRange();
745bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    hadError = true;
7464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
7474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
748bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    return;
7490cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
7500cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
7510cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
752930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
753930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &Index,
754930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         InitListExpr *StructuredList,
755930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &StructuredIndex) {
756930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  if (Index < IList->getNumInits()) {
757930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    Expr *expr = IList->getInit(Index);
758930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (isa<InitListExpr>(expr)) {
7590820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
760930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        << DeclType << IList->getSourceRange();
761930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
762930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
763930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
764930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      return;
765930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
766930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
767930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
7680820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (SemaRef.CheckReferenceInit(expr, DeclType))
769930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
770930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    else if (savExpr != expr) {
771930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // The type was promoted, update initializer list.
772930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      IList->setInit(Index, expr);
773930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
774930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (hadError)
775930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
776930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    else
777930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
778930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
779930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else {
780390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: It would be wonderful if we could point at the actual member. In
781390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // general, it would be useful to pass location information down the stack,
782390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // so that we know the location (or decl) of the "current object" being
783390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // initialized.
7840820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(),
785930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                  diag::err_init_reference_member_uninitialized)
786930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << DeclType
787930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << IList->getSourceRange();
788930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
789930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
790930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++StructuredIndex;
791930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    return;
792930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
793930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor}
794930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
7950cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
7964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
7974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
7984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
7990cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
8000cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    const VectorType *VT = DeclType->getAsVectorType();
8010cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    int maxElements = VT->getNumElements();
8020cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    QualType elementType = VT->getElementType();
8030cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
8040cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    for (int i = 0; i < maxElements; ++i) {
8050cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      // Don't attempt to go past the end of the init list
8060cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      if (Index >= IList->getNumInits())
8070cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff        break;
8086fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      CheckSubElementType(IList, elementType, Index,
8094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          StructuredList, StructuredIndex);
8100cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
8110cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
8120cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
8130cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
8140cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
81587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                     llvm::APSInt elementIndex,
81687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                     bool SubobjectIsDesignatorContext,
8174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &Index,
8184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     InitListExpr *StructuredList,
8194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &StructuredIndex) {
8200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  // Check for the special-case of initializing an array with a string.
8210cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
82279e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
82379e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner                                 SemaRef.Context)) {
82479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner      CheckStringInit(Str, DeclType, SemaRef);
8254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // We place the string literal directly into the resulting
8264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // initializer list. This is the only place where the structure
8274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // of the structured initializer list doesn't match exactly,
8284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // because doing so would involve allocating one character
8294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // constant for each string.
830f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner      UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
8310820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
8320cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      ++Index;
8330cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      return;
8340cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
8350cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
836c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const VariableArrayType *VAT =
8370820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsVariableArrayType(DeclType)) {
838638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // Check for VLAs; in standard C it would be possible to check this
839638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // earlier, but I don't know where clang accepts VLAs (gcc accepts
840638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // them in all sorts of strange places).
8410820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
842dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_variable_object_no_init)
843dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << VAT->getSizeExpr()->getSourceRange();
844638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    hadError = true;
8454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
8464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
847638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
848638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  }
849638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman
85005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // We might know the maximum number of elements in advance.
8514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  llvm::APSInt maxElements(elementIndex.getBitWidth(),
8524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           elementIndex.isUnsigned());
85305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool maxElementsKnown = false;
85405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (const ConstantArrayType *CAT =
8550820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
85605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElements = CAT->getSize();
857f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor    elementIndex.extOrTrunc(maxElements.getBitWidth());
858e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    elementIndex.setIsUnsigned(maxElements.isUnsigned());
85905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElementsKnown = true;
86005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
86105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
8620820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
863c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                             ->getElementType();
86405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
86505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
86605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
86787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
86887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
86987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
87087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
87187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
87287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
87387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. elementIndex will be
87487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // updated to be the next array element we'll initialize.
875711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if (CheckDesignatedInitializer(IList, DIE, 0,
8764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, 0, &elementIndex, Index,
877eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex, true,
878eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     false)) {
87905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
88087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        continue;
88187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      }
88287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
883f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      if (elementIndex.getBitWidth() > maxElements.getBitWidth())
884f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor        maxElements.extend(elementIndex.getBitWidth());
885f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
886f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor        elementIndex.extend(maxElements.getBitWidth());
887e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor      elementIndex.setIsUnsigned(maxElements.isUnsigned());
888f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor
88987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If the array is of incomplete type, keep track of the number of
89087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // elements in the initializer.
89187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!maxElementsKnown && elementIndex > maxElements)
89287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        maxElements = elementIndex;
89305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
89405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
89505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
89605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
89705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we know the maximum number of elements, and we've already
89805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // hit it, stop consuming elements in the initializer list.
89905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElementsKnown && elementIndex == maxElements)
9000cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      break;
90105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
90205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // Check this element.
9036fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, elementType, Index,
9044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
90505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++elementIndex;
90605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
90705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If the array is of incomplete type, keep track of the number of
90805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // elements in the initializer.
90905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (!maxElementsKnown && elementIndex > maxElements)
91005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      maxElements = elementIndex;
9110cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
912587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman  if (!hadError && DeclType->isIncompleteArrayType()) {
9130cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // If this is an incomplete array type, the actual type needs to
914396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar    // be calculated here.
915e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
91605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElements == Zero) {
917396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // Sizing an array implicitly to zero is not allowed by ISO C,
918396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // but is supported by GNU.
9190820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
920396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                    diag::ext_typecheck_zero_array_size);
9210cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
922396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar
9230820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
924396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                                                     ArrayType::Normal, 0);
9250cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
9260cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
9270cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
9280cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffvoid InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
9290cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff                                            QualType DeclType,
93087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            RecordDecl::field_iterator Field,
93187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            bool SubobjectIsDesignatorContext,
9324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
9334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
934eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
935eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
936b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
9370cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
938b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // If the record is invalid, some of it's members are invalid. To avoid
939b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // confusion, we forgo checking the intializer for the entire record.
940b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  if (structDecl->isInvalidDecl()) {
941b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    hadError = true;
942b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    return;
943b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  }
9443498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
9453498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  if (DeclType->isUnionType() && IList->getNumInits() == 0) {
9463498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    // Value-initialize the first named member of the union.
9473498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
9486ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    for (RecordDecl::field_iterator FieldEnd = RD->field_end(SemaRef.Context);
9493498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor         Field != FieldEnd; ++Field) {
9503498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      if (Field->getDeclName()) {
9513498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor        StructuredList->setInitializedFieldInUnion(*Field);
9523498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor        break;
9533498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      }
9543498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    }
9553498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return;
9563498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  }
9573498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
95805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // If structDecl is a forward declaration, this loop won't do
95905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // anything except look at designated initializers; That's okay,
96005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // because an error should get printed out elsewhere. It might be
96105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // worthwhile to skip over the rest of the initializer, though.
96244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
9636ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  RecordDecl::field_iterator FieldEnd = RD->field_end(SemaRef.Context);
964dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor  bool InitializedSomething = false;
96505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
96605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
96705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
96805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
96987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
97087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
97187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
97287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
97387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
97487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
97587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. Field will be updated to
97687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the next field that we'll be initializing.
977711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if (CheckDesignatedInitializer(IList, DIE, 0,
9784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, &Field, 0, Index,
979eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex,
980eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, TopLevelObject))
98105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
98205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
983dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      InitializedSomething = true;
98405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
98505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
98605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
98705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field == FieldEnd) {
98805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      // We've run out of fields. We're done.
98944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      break;
99005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
99144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
992dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    // We've already initialized a member of a union. We're done.
993dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    if (InitializedSomething && DeclType->isUnionType())
994dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      break;
995dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor
99605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we've hit the flexible array member at the end, we're done.
99705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field->getType()->isIncompleteArrayType())
998b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      break;
99944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10000bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (Field->isUnnamedBitfield()) {
10014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // Don't initialize unnamed bitfields, e.g. "int : 20;"
100205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Field;
1003b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      continue;
10040cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
100544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10066fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, Field->getType(), Index,
10074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
1008dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    InitializedSomething = true;
10090bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
10100bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (DeclType->isUnionType()) {
10110bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Initialize the first field within the union.
10120bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      StructuredList->setInitializedFieldInUnion(*Field);
10130bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
101405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
101505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++Field;
10160cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
101744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1018eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
1019a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      Index >= IList->getNumInits())
1020eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    return;
1021eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1022eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  // Handle GNU flexible array initializers.
1023eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  if (!TopLevelObject &&
1024a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      (!isa<InitListExpr>(IList->getInit(Index)) ||
1025a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor       cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
10260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1027eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                  diag::err_flexible_array_init_nonempty)
1028eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      << IList->getInit(Index)->getSourceRange().getBegin();
10290820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1030eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      << *Field;
1031eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    hadError = true;
1032a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    ++Index;
1033a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    return;
1034a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  } else {
1035a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1036a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                 diag::ext_flexible_array_init)
1037a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      << IList->getInit(Index)->getSourceRange().getBegin();
1038a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1039a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      << *Field;
1040eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  }
1041eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1042a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  if (isa<InitListExpr>(IList->getInit(Index)))
1043a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1044a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                        StructuredIndex);
1045a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  else
1046a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1047a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                          StructuredIndex);
10480cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
10490cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
1050ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Expand a field designator that refers to a member of an
1051ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// anonymous struct or union into a series of field designators that
1052ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// refers to the field within the appropriate subobject.
1053ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor///
1054ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// Field/FieldIndex will be updated to point to the (new)
1055ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// currently-designated field.
1056ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregorstatic void ExpandAnonymousFieldDesignator(Sema &SemaRef,
1057ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                           DesignatedInitExpr *DIE,
1058ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                           unsigned DesigIdx,
1059ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                           FieldDecl *Field,
1060ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                        RecordDecl::field_iterator &FieldIter,
1061ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                           unsigned &FieldIndex) {
1062ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  typedef DesignatedInitExpr::Designator Designator;
1063ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1064ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Build the path from the current object to the member of the
1065ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // anonymous struct/union (backwards).
1066ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  llvm::SmallVector<FieldDecl *, 4> Path;
1067ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
1068ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1069ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Build the replacement designators.
1070ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  llvm::SmallVector<Designator, 4> Replacements;
1071ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1072ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor         FI = Path.rbegin(), FIEnd = Path.rend();
1073ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor       FI != FIEnd; ++FI) {
1074ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (FI + 1 == FIEnd)
1075ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      Replacements.push_back(Designator((IdentifierInfo *)0,
1076ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                    DIE->getDesignator(DesigIdx)->getDotLoc(),
1077ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                DIE->getDesignator(DesigIdx)->getFieldLoc()));
1078ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    else
1079ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1080ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                        SourceLocation()));
1081ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Replacements.back().setField(*FI);
1082ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
1083ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1084ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Expand the current designator into the set of replacement
1085ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // designators, so we have a full subobject path down to where the
1086ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // member of the anonymous struct/union is actually stored.
1087ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  DIE->ExpandDesignator(DesigIdx, &Replacements[0],
1088ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                        &Replacements[0] + Replacements.size());
1089ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1090ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Update FieldIter/FieldIndex;
1091ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
1092ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  FieldIter = Record->field_begin(SemaRef.Context);
1093ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  FieldIndex = 0;
1094ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  for (RecordDecl::field_iterator FEnd = Record->field_end(SemaRef.Context);
1095ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor       FieldIter != FEnd; ++FieldIter) {
1096ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (FieldIter->isUnnamedBitfield())
1097ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        continue;
1098ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1099ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (*FieldIter == Path.back())
1100ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      return;
1101ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1102ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    ++FieldIndex;
1103ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
1104ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1105ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  assert(false && "Unable to find anonymous struct/union field");
1106ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
1107ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
110805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @brief Check the well-formedness of a C99 designated initializer.
110905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
111005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Determines whether the designated initializer @p DIE, which
111105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// resides at the given @p Index within the initializer list @p
111205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// IList, is well-formed for a current object of type @p DeclType
111305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// (C99 6.7.8). The actual subobject that this designator refers to
111405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// within the current subobject is returned in either
11154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @p NextField or @p NextElementIndex (whichever is appropriate).
111605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
111705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param IList  The initializer list in which this designated
111805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// initializer occurs.
111905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
1120711997184366d584c9c437102cae1e9d9927b986Douglas Gregor/// @param DIE The designated initializer expression.
1121711997184366d584c9c437102cae1e9d9927b986Douglas Gregor///
1122711997184366d584c9c437102cae1e9d9927b986Douglas Gregor/// @param DesigIdx  The index of the current designator.
112305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
112405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param DeclType  The type of the "current object" (C99 6.7.8p17),
112505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// into which the designation in @p DIE should refer.
112605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
112787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextField  If non-NULL and the first designator in @p DIE is
112887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// a field, this will be set to the field declaration corresponding
112987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// to the field named by the designator.
113005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
113187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextElementIndex  If non-NULL and the first designator in @p
113287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// DIE is an array designator or GNU array-range designator, this
113387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// will be set to the last index initialized by this designator.
113405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
113505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param Index  Index into @p IList where the designated initializer
113605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @p DIE occurs.
113705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
11384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @param StructuredList  The initializer list expression that
11394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// describes all of the subobject initializers in the order they'll
11404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// actually be initialized.
11414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor///
114205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @returns true if there was an error, false otherwise.
114387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregorbool
114487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas GregorInitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
114587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      DesignatedInitExpr *DIE,
1146711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                      unsigned DesigIdx,
114787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      QualType &CurrentObjectType,
114887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      RecordDecl::field_iterator *NextField,
114987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      llvm::APSInt *NextElementIndex,
11504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
11514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
115234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                      unsigned &StructuredIndex,
1153eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool FinishSubobjectInit,
1154eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
1155711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  if (DesigIdx == DIE->size()) {
115687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the actual initialization for the designated object type.
115787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
11586fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
11596fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Temporarily remove the designator expression from the
11606fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // initializer list that the child calls see, so that we don't try
11616fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // to re-process the designator.
11626fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    unsigned OldIndex = Index;
11636fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE->getInit());
11646fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
11656fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, CurrentObjectType, Index,
11664c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
11676fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
11686fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Restore the designated initializer expression in the syntactic
11696fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // form of the initializer list.
11706fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    if (IList->getInit(OldIndex) != DIE->getInit())
11716fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      DIE->setInit(IList->getInit(OldIndex));
11726fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE);
11736fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
117487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
117587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
117605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1177711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  bool IsFirstDesignator = (DesigIdx == 0);
11784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  assert((IsFirstDesignator || StructuredList) &&
11794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor         "Need a non-designated initializer list to start from");
11804c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1181711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
11824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Determine the structural initializer list that corresponds to the
11834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // current subobject.
11844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
1185ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
1186ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                                 StructuredList, StructuredIndex,
11874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                 SourceRange(D->getStartLocation(),
11884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                             DIE->getSourceRange().getEnd()));
11894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  assert(StructuredList && "Expected a structured initializer list");
11904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
119187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (D->isFieldDesignator()) {
119287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // C99 6.7.8p7:
119387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
119487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   If a designator has the form
119587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
119687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //      . identifier
119787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
119887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   then the current object (defined below) shall have
119987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   structure or union type and the identifier shall be the
120087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   name of a member of that type.
120187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    const RecordType *RT = CurrentObjectType->getAsRecordType();
120287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (!RT) {
120387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      SourceLocation Loc = D->getDotLoc();
120487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (Loc.isInvalid())
120587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        Loc = D->getFieldLoc();
12060820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
12070820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
120887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
120987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
121087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
121187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
12124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Note: we perform a linear search of the fields here, despite
12134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the fact that we have a faster lookup method, because we always
12144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // need to compute the field's index.
1215ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    FieldDecl *KnownField = D->getField();
121687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IdentifierInfo *FieldName = D->getFieldName();
12174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned FieldIndex = 0;
12186ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    RecordDecl::field_iterator
12196ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      Field = RT->getDecl()->field_begin(SemaRef.Context),
12206ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      FieldEnd = RT->getDecl()->field_end(SemaRef.Context);
12214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    for (; Field != FieldEnd; ++Field) {
12224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->isUnnamedBitfield())
12234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        continue;
12244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1225ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      if (KnownField == *Field || Field->getIdentifier() == FieldName)
12264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        break;
12274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
12284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++FieldIndex;
122987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
123087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
12314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (Field == FieldEnd) {
1232ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // There was no normal field in the struct with the designated
1233ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // name. Perform another lookup for this name, which may find
1234ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // something that we can't designate (e.g., a member function),
1235ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // may find nothing, or may find a member of an anonymous
1236ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // struct/union.
12376ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      DeclContext::lookup_result Lookup
12386ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor        = RT->getDecl()->lookup(SemaRef.Context, FieldName);
12394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Lookup.first == Lookup.second) {
12404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        // Name lookup didn't find anything.
12410820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
12424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor          << FieldName << CurrentObjectType;
1243ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        ++Index;
1244ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        return true;
1245ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      } else if (!KnownField && isa<FieldDecl>(*Lookup.first) &&
1246ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                 cast<RecordDecl>((*Lookup.first)->getDeclContext())
1247ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                   ->isAnonymousStructOrUnion()) {
1248ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        // Handle an field designator that refers to a member of an
1249ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        // anonymous struct or union.
1250ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
1251ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       cast<FieldDecl>(*Lookup.first),
1252ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       Field, FieldIndex);
1253ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        D = DIE->getDesignator(DesigIdx);
12544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      } else {
12554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        // Name lookup found something, but it wasn't a field.
12560820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
12574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor          << FieldName;
12580820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag((*Lookup.first)->getLocation(),
12594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                      diag::note_field_designator_found);
1260ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        ++Index;
1261ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        return true;
1262ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      }
1263ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    } else if (!KnownField &&
1264ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor               cast<RecordDecl>((*Field)->getDeclContext())
12654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 ->isAnonymousStructOrUnion()) {
1266ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1267ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                     Field, FieldIndex);
1268ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      D = DIE->getDesignator(DesigIdx);
126987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
12704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
12714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // All of the fields of a union are located at the same place in
12724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the initializer list.
12730bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (RT->getDecl()->isUnion()) {
12744c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      FieldIndex = 0;
12750bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      StructuredList->setInitializedFieldInUnion(*Field);
12760bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
12774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
127887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Update the designator with the field declaration.
12794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    D->setField(*Field);
128005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
12814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Make sure that our non-designated initializer list has space
12824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // for a subobject corresponding to this field.
12834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (FieldIndex >= StructuredList->getNumInits())
12840820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
12854c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1286eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    // This designator names a flexible array member.
1287eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    if (Field->getType()->isIncompleteArrayType()) {
1288eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool Invalid = false;
1289711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if ((DesigIdx + 1) != DIE->size()) {
1290eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // We can't designate an object within the flexible array
1291eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // member (because GCC doesn't allow it).
1292711997184366d584c9c437102cae1e9d9927b986Douglas Gregor        DesignatedInitExpr::Designator *NextD
1293711997184366d584c9c437102cae1e9d9927b986Douglas Gregor          = DIE->getDesignator(DesigIdx + 1);
12940820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(NextD->getStartLocation(),
1295eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_designator_into_flexible_array_member)
1296eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << SourceRange(NextD->getStartLocation(),
1297eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                         DIE->getSourceRange().getEnd());
12980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1299eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1300eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1301eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1302eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1303eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1304eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // The initializer is not an initializer list.
13050820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
1306eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_flexible_array_init_needs_braces)
1307eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << DIE->getInit()->getSourceRange();
13080820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1309eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1310eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1311eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1312eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1313eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Handle GNU flexible array initializers.
1314eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (!Invalid && !TopLevelObject &&
1315eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
13160820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(DIE->getSourceRange().getBegin(),
1317eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_flexible_array_init_nonempty)
1318eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << DIE->getSourceRange().getBegin();
13190820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1320eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1321eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1322eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1323eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1324eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (Invalid) {
1325eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Index;
1326eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1327eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1328eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1329eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Initialize the array.
1330eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool prevHadError = hadError;
1331eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1332eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned OldIndex = Index;
1333eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(Index, DIE->getInit());
1334eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      CheckSubElementType(IList, Field->getType(), Index,
1335eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                          StructuredList, newStructuredIndex);
1336eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(OldIndex, DIE);
1337eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (hadError && !prevHadError) {
1338eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Field;
1339eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++FieldIndex;
1340eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        if (NextField)
1341eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          *NextField = Field;
1342eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        StructuredIndex = FieldIndex;
1343eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1344eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1345eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    } else {
1346eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Recurse to check later designated subobjects.
1347eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType FieldType = (*Field)->getType();
1348eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1349711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
1350711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                     Index, StructuredList, newStructuredIndex,
1351eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, false))
1352eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1353eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    }
135487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
135587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Find the position of the next field to be initialized in this
135687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // subobject.
135787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Field;
13584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++FieldIndex;
135905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
136087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // If this the first designator, our caller will continue checking
136187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // the rest of this struct/class/union subobject.
136287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (IsFirstDesignator) {
136387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (NextField)
136487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        *NextField = Field;
13654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      StructuredIndex = FieldIndex;
136687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return false;
136787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
136805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
136934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (!FinishSubobjectInit)
137034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return false;
137134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
1372ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    // We've already initialized something in the union; we're done.
1373ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (RT->getDecl()->isUnion())
1374ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      return hadError;
1375ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
137687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the remaining fields within this class/struct/union subobject.
137787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
13784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
13794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          StructuredList, FieldIndex);
138087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
138187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
138205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
138387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // C99 6.7.8p6:
138487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
138587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   If a designator has the form
138687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
138787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ]
138887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
138987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   then the current object (defined below) shall have array
139087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   type and the expression shall be an integer constant
139187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   expression. If the array is of unknown size, any
139287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   nonnegative value is valid.
139387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
139487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Additionally, cope with the GNU extension that permits
139587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // designators of the form
139687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
139787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ... constant-expression ]
13980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
139987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (!AT) {
14000820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
140187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      << CurrentObjectType;
140287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Index;
140387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return true;
140487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
140505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
140687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  Expr *IndexExpr = 0;
140734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
140834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (D->isArrayDesignator()) {
140987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayIndex(*D);
14103bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
141134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex = DesignatedStartIndex;
141234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
141387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    assert(D->isArrayRangeDesignator() && "Need array-range designator");
14143bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner
141534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
14163bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    DesignatedStartIndex =
14173bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner      DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
14183bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    DesignatedEndIndex =
14193bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner      DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
142087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayRangeEnd(*D);
142134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
14223bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
1423a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor      FullyStructuredList->sawArrayRangeDesignator();
142487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
142505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
142687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (isa<ConstantArrayType>(AT)) {
142787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
142834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
142934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
143034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
143134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
143234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedEndIndex >= MaxElements) {
14330820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
143487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                    diag::err_array_designator_too_large)
143534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor        << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
143687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        << IndexExpr->getSourceRange();
143787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
143887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
143905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
144034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
144134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Make sure the bit-widths and signedness match.
144234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
144334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
14443bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    else if (DesignatedStartIndex.getBitWidth() <
14453bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner             DesignatedEndIndex.getBitWidth())
144634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
144734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(true);
144834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(true);
144905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
145087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
14514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Make sure that our non-designated initializer list has space
14524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // for a subobject corresponding to this array element.
145334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
14540820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    StructuredList->resizeInits(SemaRef.Context,
145534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                DesignatedEndIndex.getZExtValue() + 1);
145634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
145734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Repeatedly perform subobject initializations in the range
145834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // [DesignatedStartIndex, DesignatedEndIndex].
145934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
146034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Move to the next designator
146134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
146234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned OldIndex = Index;
146334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  while (DesignatedStartIndex <= DesignatedEndIndex) {
146434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Recurse to check later designated subobjects.
146534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    QualType ElementType = AT->getElementType();
146634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    Index = OldIndex;
1467711997184366d584c9c437102cae1e9d9927b986Douglas Gregor    if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
1468711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                   Index, StructuredList, ElementIndex,
1469eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   (DesignatedStartIndex == DesignatedEndIndex),
1470eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   false))
147134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return true;
147287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
147334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Move to the next index in the array that we'll be initializing.
147434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ++DesignatedStartIndex;
147534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ElementIndex = DesignatedStartIndex.getZExtValue();
147634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  }
147787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
147887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // If this the first designator, our caller will continue checking
147987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // the rest of this array subobject.
148087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (IsFirstDesignator) {
148187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (NextElementIndex)
148234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      *NextElementIndex = DesignatedStartIndex;
14834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredIndex = ElementIndex;
148487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return false;
148587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
148634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
148734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (!FinishSubobjectInit)
148834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    return false;
148934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
149087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Check the remaining elements within this array subobject.
149105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool prevHadError = hadError;
1492fdf556936f94344d5482747403f27822cf0ae37fDouglas Gregor  CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
14934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 StructuredList, ElementIndex);
149487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  return hadError && !prevHadError;
149505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
149605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
14974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// Get the structured initializer list for a subobject of type
14984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// @p CurrentObjectType.
14994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListExpr *
15004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
15014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType CurrentObjectType,
15024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
15034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned StructuredIndex,
15044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            SourceRange InitRange) {
15054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *ExistingInit = 0;
15064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
15074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = SyntacticToSemantic[IList];
15084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else if (StructuredIndex < StructuredList->getNumInits())
15094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = StructuredList->getInit(StructuredIndex);
15104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
15124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return Result;
15134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (ExistingInit) {
15154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // We are creating an initializer list that initializes the
15164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobjects of the current object, but there was already an
15174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // initialization that completely initialized the current
15184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject, e.g., by a compound literal:
15194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    //
15204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X { int a, b; };
15214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
15224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    //
15234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
15244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // designated initializer re-initializes the whole
15254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject [0], overwriting previous initializers.
1526ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    SemaRef.Diag(InitRange.getBegin(),
1527ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                 diag::warn_subobject_initializer_overrides)
15284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << InitRange;
15290820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
15304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
153154f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
15324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << ExistingInit->getSourceRange();
15334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
15344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitListExpr *Result
1536ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
1537ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                                         InitRange.getEnd());
1538ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor
15394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Result->setType(CurrentObjectType);
15404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1541fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  // Pre-allocate storage for the structured initializer list.
1542fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  unsigned NumElements = 0;
154308457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  unsigned NumInits = 0;
154408457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  if (!StructuredList)
154508457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    NumInits = IList->getNumInits();
154608457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  else if (Index < IList->getNumInits()) {
154708457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
154808457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      NumInits = SubList->getNumInits();
154908457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  }
155008457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor
1551fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  if (const ArrayType *AType
1552fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1553fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1554fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = CAType->getSize().getZExtValue();
1555fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // Simple heuristic so that we don't allocate a very large
1556fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // initializer with many empty entries at the end.
155708457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      if (NumInits && NumElements > NumInits)
1558fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor        NumElements = 0;
1559fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    }
1560fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  } else if (const VectorType *VType = CurrentObjectType->getAsVectorType())
1561fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = VType->getNumElements();
1562fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  else if (const RecordType *RType = CurrentObjectType->getAsRecordType()) {
1563fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    RecordDecl *RDecl = RType->getDecl();
1564fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (RDecl->isUnion())
1565fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = 1;
1566fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    else
15676ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      NumElements = std::distance(RDecl->field_begin(SemaRef.Context),
15686ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                  RDecl->field_end(SemaRef.Context));
1569fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  }
1570fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
157108457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  if (NumElements < NumInits)
1572fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = IList->getNumInits();
1573fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
1574fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  Result->reserveInits(NumElements);
1575fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
15764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Link this new initializer list into the structured initializer
15774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // lists.
15784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (StructuredList)
15794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredList->updateInit(StructuredIndex, Result);
15804c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else {
15814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    Result->setSyntacticForm(IList);
15824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    SyntacticToSemantic[IList] = Result;
15834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
15844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15854c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
15864c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
15874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Update the initializer at index @p StructuredIndex within the
15894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// structured initializer list to the value @p expr.
15904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
15914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  unsigned &StructuredIndex,
15924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  Expr *expr) {
15934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // No structured initializer list to update
15944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
15954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
15964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
15974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
15984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // This initializer overwrites a previous initializer. Warn.
15990820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(expr->getSourceRange().getBegin(),
16004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::warn_initializer_overrides)
16014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << expr->getSourceRange();
16020820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
16034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
160454f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
16054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << PrevInit->getSourceRange();
16064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
16074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
16084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  ++StructuredIndex;
16094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
16104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
161105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Check that the given Index expression is a valid array designator
161205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// value. This is essentailly just a wrapper around
16133bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner/// VerifyIntegerConstantExpression that also checks for negative values
161405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// and produces a reasonable diagnostic if there is a
161505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// failure. Returns true if there was an error, false otherwise.  If
161605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// everything went okay, Value will receive the value of the constant
161705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// expression.
161805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregorstatic bool
16193bf6893b77c30cb774100e0fa7ae029331675ec1Chris LattnerCheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
162005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation Loc = Index->getSourceRange().getBegin();
162105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
162205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Make sure this is an integer constant expression.
16233bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner  if (S.VerifyIntegerConstantExpression(Index, &Value))
16243bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    return true;
16253bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner
16263bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner  if (Value.isSigned() && Value.isNegative())
16273bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    return S.Diag(Loc, diag::err_array_designator_negative)
162805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      << Value.toString(10) << Index->getSourceRange();
162905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
163053d3d8e0662197f7245d8f5ff697a72a2b4b3f54Douglas Gregor  Value.setIsUnsigned(true);
163105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return false;
163205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
163305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
163405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
163505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                                        SourceLocation Loc,
1636eeae8f072748affce25ab4064982626361293390Douglas Gregor                                                        bool GNUSyntax,
163705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                                        OwningExprResult Init) {
163805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  typedef DesignatedInitExpr::Designator ASTDesignator;
163905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
164005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool Invalid = false;
164105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  llvm::SmallVector<ASTDesignator, 32> Designators;
164205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  llvm::SmallVector<Expr *, 32> InitExpressions;
164305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
164405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Build designators and check array designator expressions.
164505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
164605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    const Designator &D = Desig.getDesignator(Idx);
164705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    switch (D.getKind()) {
164805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::FieldDesignator:
164905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
165005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                          D.getFieldLoc()));
165105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
165205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
165305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayDesignator: {
165405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *Index = static_cast<Expr *>(D.getArrayIndex());
165505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt IndexValue;
16569ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      if (!Index->isTypeDependent() &&
16579ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          !Index->isValueDependent() &&
16589ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          CheckArrayDesignatorExpr(*this, Index, IndexValue))
165905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
166005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      else {
166105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Designators.push_back(ASTDesignator(InitExpressions.size(),
166205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                            D.getLBracketLoc(),
166305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                            D.getRBracketLoc()));
166405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        InitExpressions.push_back(Index);
166505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
166605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
166705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
166805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
166905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayRangeDesignator: {
167005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
167105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
167205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt StartValue;
167305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt EndValue;
16749ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      bool StartDependent = StartIndex->isTypeDependent() ||
16759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                            StartIndex->isValueDependent();
16769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      bool EndDependent = EndIndex->isTypeDependent() ||
16779ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                          EndIndex->isValueDependent();
16789ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      if ((!StartDependent &&
16799ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor           CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
16809ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          (!EndDependent &&
16819ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor           CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
168205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
1683d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor      else {
1684d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        // Make sure we're comparing values with the same bit width.
16859ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        if (StartDependent || EndDependent) {
16869ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          // Nothing to compute.
16879ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
1688d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          EndValue.extend(StartValue.getBitWidth());
1689d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1690d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          StartValue.extend(EndValue.getBitWidth());
1691d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor
1692c4bb7bfe8328f79004517dd268f6146c8066c205Douglas Gregor        if (!StartDependent && !EndDependent && EndValue < StartValue) {
1693d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1694d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor            << StartValue.toString(10) << EndValue.toString(10)
1695d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor            << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1696d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Invalid = true;
1697d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        } else {
1698d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Designators.push_back(ASTDesignator(InitExpressions.size(),
1699d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getLBracketLoc(),
1700d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getEllipsisLoc(),
1701d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getRBracketLoc()));
1702d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(StartIndex);
1703d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(EndIndex);
1704d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        }
170505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
170605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
170705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
170805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
170905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
171005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
171105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Invalid || Init.isInvalid())
171205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return ExprError();
171305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
171405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Clear out the expressions within the designation.
171505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Desig.ClearExprs(*this);
171605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
171705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  DesignatedInitExpr *DIE
1718beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    = DesignatedInitExpr::Create(Context,
1719beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                 Designators.data(), Designators.size(),
1720beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                 InitExpressions.data(), InitExpressions.size(),
1721e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                 Loc, GNUSyntax, Init.takeAs<Expr>());
172205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return Owned(DIE);
172305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
1724c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
1725c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorbool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
17260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  InitListChecker CheckInitList(*this, InitList, DeclType);
1727c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  if (!CheckInitList.HadError())
1728c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor    InitList = CheckInitList.getFullyStructuredList();
1729c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
1730c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  return CheckInitList.HadError();
1731c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor}
173287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
173387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// \brief Diagnose any semantic errors with value-initialization of
173487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// the given type.
173587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor///
173687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// Value-initialization effectively zero-initializes any types
173787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// without user-declared constructors, and calls the default
173887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// constructor for a for any type that has a user-declared
173987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
174087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// a type with a user-declared constructor does not have an
174187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// accessible, non-deleted default constructor. In C, everything can
174287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// be value-initialized, which corresponds to C's notion of
174387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// initializing objects with static storage duration when no
174487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// initializer is provided for that object.
174587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor///
174687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor/// \returns true if there was an error, false otherwise.
174787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregorbool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
174887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  // C++ [dcl.init]p5:
174987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  //
175087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  //   To value-initialize an object of type T means:
175187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
175287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  //     -- if T is an array type, then each element is value-initialized;
175387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (const ArrayType *AT = Context.getAsArrayType(Type))
175487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    return CheckValueInitialization(AT->getElementType(), Loc);
175587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
175687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (const RecordType *RT = Type->getAsRecordType()) {
1757c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
175887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      // -- if T is a class type (clause 9) with a user-declared
175987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      //    constructor (12.1), then the default constructor for T is
176087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      //    called (and the initialization is ill-formed if T has no
176187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      //    accessible default constructor);
1762c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor      if (ClassDecl->hasUserDeclaredConstructor())
1763390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // FIXME: Eventually, we'll need to put the constructor decl into the
1764390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // AST.
176587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        return PerformInitializationByConstructor(Type, 0, 0, Loc,
176687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor                                                  SourceRange(Loc),
176787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor                                                  DeclarationName(),
176887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor                                                  IK_Direct);
176987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    }
177087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
177187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
177287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (Type->isReferenceType()) {
177387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    // C++ [dcl.init]p5:
177487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    //   [...] A program that calls for default-initialization or
177587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    //   value-initialization of an entity of reference type is
177687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    //   ill-formed. [...]
1777390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we have code that goes through this path, add an actual
1778390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // diagnostic :)
177987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
178087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
178187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  return false;
178287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor}
1783