SemaInit.cpp revision c07b8c02f7c54631ab9a9bd7db9f031d3db170ca
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
1820093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include "SemaInit.h"
19c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor#include "Lookup.h"
200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff#include "Sema.h"
2105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor#include "clang/Parse/Designator.h"
220cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff#include "clang/AST/ASTContext.h"
232078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson#include "clang/AST/ExprCXX.h"
2479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner#include "clang/AST/ExprObjC.h"
25d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor#include "clang/AST/TypeLoc.h"
2620093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include "llvm/Support/ErrorHandling.h"
27c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor#include <map>
2805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregorusing namespace clang;
290cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
30dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
31dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Sema Initialization Checking
32dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
33dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
3479e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattnerstatic Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
358879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  const ArrayType *AT = Context.getAsArrayType(DeclType);
368879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (!AT) return 0;
378879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
388718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
398718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman    return 0;
408718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman
418879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // See if this is a string literal or @encode.
428879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  Init = Init->IgnoreParens();
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
448879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Handle @encode, which is a narrow string.
458879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
468879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
478879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
488879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Otherwise we can only handle string literals.
498879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  StringLiteral *SL = dyn_cast<StringLiteral>(Init);
50220b6369d7717bfe6894b46cef055d3e763827f2Chris Lattner  if (SL == 0) return 0;
51bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman
52bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  QualType ElemTy = Context.getCanonicalType(AT->getElementType());
538879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // char array can be initialized with a narrow string.
548879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Only allow char x[] = "foo";  not char x[] = L"foo";
558879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (!SL->isWide())
56bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman    return ElemTy->isCharType() ? Init : 0;
578879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
58bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
59bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  // correction from DR343): "An array with element type compatible with a
60bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  // qualified or unqualified version of wchar_t may be initialized by a wide
61bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  // string literal, optionally enclosed in braces."
62bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  if (Context.typesAreCompatible(Context.getWCharType(),
63bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman                                 ElemTy.getUnqualifiedType()))
648879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  return 0;
67dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
68dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
69c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlssonstatic Sema::OwningExprResult
70c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders CarlssonCheckSingleInitializer(const InitializedEntity *Entity,
71c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                       Sema::OwningExprResult Init, QualType DeclType, Sema &S){
72c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson  Expr *InitExpr = Init.takeAs<Expr>();
73c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
74dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // Get the type before calling CheckSingleAssignmentConstraints(), since
75dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  // it can promote the expression.
76c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson  QualType InitType = InitExpr->getType();
771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7895e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  if (S.getLangOptions().CPlusPlus) {
79dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // FIXME: I dislike this error message. A lot.
80c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    if (S.PerformImplicitConversion(InitExpr, DeclType,
81c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                                    Sema::AA_Initializing,
82c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                                    /*DirectInit=*/false)) {
8334acd3e62c4ca3a2904f2515ff2a1a94c7d27b01Fariborz Jahanian      ImplicitConversionSequence ICS;
8434acd3e62c4ca3a2904f2515ff2a1a94c7d27b01Fariborz Jahanian      OverloadCandidateSet CandidateSet;
85c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      if (S.IsUserDefinedConversion(InitExpr, DeclType, ICS.UserDefined,
8634acd3e62c4ca3a2904f2515ff2a1a94c7d27b01Fariborz Jahanian                              CandidateSet,
87c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                              true, false, false) != OR_Ambiguous) {
88c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson        S.Diag(InitExpr->getSourceRange().getBegin(),
8934acd3e62c4ca3a2904f2515ff2a1a94c7d27b01Fariborz Jahanian                      diag::err_typecheck_convert_incompatible)
90c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                      << DeclType << InitExpr->getType() << Sema::AA_Initializing
91c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                      << InitExpr->getSourceRange();
92c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson        return S.ExprError();
93c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      }
94c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      S.Diag(InitExpr->getSourceRange().getBegin(),
9534acd3e62c4ca3a2904f2515ff2a1a94c7d27b01Fariborz Jahanian             diag::err_typecheck_convert_ambiguous)
96c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson            << DeclType << InitExpr->getType() << InitExpr->getSourceRange();
97c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      S.PrintOverloadCandidates(CandidateSet, Sema::OCD_AllCandidates,
98c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                                &InitExpr, 1);
99c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
100c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      return S.ExprError();
10134acd3e62c4ca3a2904f2515ff2a1a94c7d27b01Fariborz Jahanian    }
102c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
103c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    Init.release();
104c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    return S.Owned(InitExpr);
105dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10795e8d658fbea5b53c5e77c48f883fe6c9f7f620fChris Lattner  Sema::AssignConvertType ConvTy =
108c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    S.CheckSingleAssignmentConstraints(DeclType, InitExpr);
109c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson  if (S.DiagnoseAssignmentResult(ConvTy, InitExpr->getLocStart(), DeclType,
110c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson                                 InitType, InitExpr, Sema::AA_Initializing))
111c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    return S.ExprError();
112c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
113c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson  Init.release();
114c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson  return S.Owned(InitExpr);
115dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
116dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
11779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattnerstatic void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
11879e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  // Get the length of the string as parsed.
11979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  uint64_t StrLength =
12079e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
12179e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner
1221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12379e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  const ArrayType *AT = S.Context.getAsArrayType(DeclT);
124dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // C99 6.7.8p14. We have an array of character type with unknown size
126dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // being initialized to a string literal.
127dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    llvm::APSInt ConstVal(32);
12819da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    ConstVal = StrLength;
129dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // Return a new array type (C99 6.7.8p22).
13046a617a792bfab0d9b1e057371ea3b9540802226John McCall    DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
13146a617a792bfab0d9b1e057371ea3b9540802226John McCall                                           ConstVal,
13246a617a792bfab0d9b1e057371ea3b9540802226John McCall                                           ArrayType::Normal, 0);
13319da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    return;
134dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1368718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1388718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // C99 6.7.8p14. We have an array of character type with known size.  However,
1398718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // the size may be smaller or larger than the string we are initializing.
1408718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // FIXME: Avoid truncation for 64-bit length strings.
1418718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  if (StrLength-1 > CAT->getSize().getZExtValue())
1428718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman    S.Diag(Str->getSourceRange().getBegin(),
1438718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman           diag::warn_initializer_string_for_char_array_too_long)
1448718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman      << Str->getSourceRange();
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1468718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // Set the type to the actual size that we are initializing.  If we have
1478718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // something like:
1488718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  //   char x[1] = "foo";
1498718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // then this will set the string literal's type to char[1].
1508718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  Str->setType(DeclT);
151dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
152dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
153dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
154dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Semantic checking for initializer lists.
155dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
156dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
1579e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// @brief Semantic checking for initializer lists.
1589e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
1599e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// The InitListChecker class contains a set of routines that each
1609e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// handle the initialization of a certain kind of entity, e.g.,
1619e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arrays, vectors, struct/union types, scalars, etc. The
1629e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// InitListChecker itself performs a recursive walk of the subobject
1639e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure of the type to be initialized, while stepping through
1649e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// the initializer list one element at a time. The IList and Index
1659e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// parameters to each of the Check* routines contain the active
1669e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// (syntactic) initializer list and the index into that initializer
1679e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list that represents the current initializer. Each routine is
1689e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// responsible for moving that Index forward as it consumes elements.
1699e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
1709e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// Each Check* routine also has a StructuredList/StructuredIndex
1719e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arguments, which contains the current the "structured" (semantic)
1729e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initializer list and the index into that initializer list where we
1739e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// are copying initializers as we map them over to the semantic
1749e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list. Once we have completed our recursive walk of the subobject
1759e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure, we will have constructed a full semantic initializer
1769e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list.
1779e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
1789e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// C99 designators cause changes in the initializer list traversal,
1799e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// because they make the initialization "jump" into a specific
1809e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// subobject and then continue the initialization from that
1819e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// point. CheckDesignatedInitializer() recursively steps into the
1829e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// designated subobject and manages backing out the recursion to
1839e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initialize the subobjects after the one designated.
1848b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattnernamespace {
185c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorclass InitListChecker {
1860820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  Sema &SemaRef;
187c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool hadError;
188c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
189c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *FullyStructuredList;
1901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
1929e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
193eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
194eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
195c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckExplicitInitList(InitListExpr *IList, QualType &T,
1969e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
197eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
198eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             bool SubobjectIsDesignatorContext,
201c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             unsigned &Index,
2029e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
203eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
204eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckSubElementType(InitListExpr *IList, QualType ElemType,
206c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                           unsigned &Index,
2079e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           InitListExpr *StructuredList,
2089e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           unsigned &StructuredIndex);
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckScalarType(InitListExpr *IList, QualType DeclType,
210c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                       unsigned &Index,
2119e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2129e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckReferenceType(InitListExpr *IList, QualType DeclType,
214930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &Index,
215930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          InitListExpr *StructuredList,
216930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &StructuredIndex);
217c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
2189e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2199e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             RecordDecl::field_iterator Field,
222c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             bool SubobjectIsDesignatorContext, unsigned &Index,
2239e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
224eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
225eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void CheckArrayType(InitListExpr *IList, QualType &DeclType,
2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      llvm::APSInt elementIndex,
228c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                      bool SubobjectIsDesignatorContext, unsigned &Index,
2299e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      InitListExpr *StructuredList,
2309e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      unsigned &StructuredIndex);
2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
232711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                  unsigned DesigIdx,
2331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                  QualType &CurrentObjectType,
234c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  RecordDecl::field_iterator *NextField,
235c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  llvm::APSInt *NextElementIndex,
236c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &Index,
237c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  InitListExpr *StructuredList,
238c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &StructuredIndex,
239eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool FinishSubobjectInit,
240eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool TopLevelObject);
241c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
242c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           QualType CurrentObjectType,
243c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           InitListExpr *StructuredList,
244c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           unsigned StructuredIndex,
245c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           SourceRange InitRange);
2469e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor  void UpdateStructuredListElement(InitListExpr *StructuredList,
2479e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                                   unsigned &StructuredIndex,
248c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                   Expr *expr);
249c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numArrayElements(QualType DeclType);
250c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numStructUnionElements(QualType DeclType);
251930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
252d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  void FillInValueInitForField(unsigned Init, FieldDecl *Field,
253d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                               const InitializedEntity &ParentEntity,
254d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                               InitListExpr *ILE, bool &RequiresSecondPass);
255cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  void FillInValueInitializations(const InitializedEntity &Entity,
256cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                  InitListExpr *ILE, bool &RequiresSecondPass);
257c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorpublic:
258cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitListChecker(Sema &S, const InitializedEntity &Entity,
259cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                  InitListExpr *IL, QualType &T);
260c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool HadError() { return hadError; }
261c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
262c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // @brief Retrieves the fully-structured initializer list used for
263c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // semantic analysis and code generation.
264c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
265c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor};
2668b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattner} // end anonymous namespace
26768355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
268d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregorvoid InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
269d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                        const InitializedEntity &ParentEntity,
270d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                              InitListExpr *ILE,
271d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                              bool &RequiresSecondPass) {
272d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  SourceLocation Loc = ILE->getSourceRange().getBegin();
273d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  unsigned NumInits = ILE->getNumInits();
274d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  InitializedEntity MemberEntity
275d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    = InitializedEntity::InitializeMember(Field, &ParentEntity);
276d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  if (Init >= NumInits || !ILE->getInit(Init)) {
277d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    // FIXME: We probably don't need to handle references
278d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    // specially here, since value-initialization of references is
279d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    // handled in InitializationSequence.
280d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (Field->getType()->isReferenceType()) {
281d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // C++ [dcl.init.aggr]p9:
282d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      //   If an incomplete or empty initializer-list leaves a
283d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      //   member of reference type uninitialized, the program is
284d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      //   ill-formed.
285d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
286d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        << Field->getType()
287d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        << ILE->getSyntacticForm()->getSourceRange();
288d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      SemaRef.Diag(Field->getLocation(),
289d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                   diag::note_uninit_reference_member);
290d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      hadError = true;
291d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      return;
292d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
293d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
294d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
295d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                                              true);
296d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
297d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (!InitSeq) {
298d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
299d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      hadError = true;
300d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      return;
301d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
302d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
303d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    Sema::OwningExprResult MemberInit
304d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      = InitSeq.Perform(SemaRef, MemberEntity, Kind,
305d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                        Sema::MultiExprArg(SemaRef, 0, 0));
306d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (MemberInit.isInvalid()) {
307d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      hadError = true;
308d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      return;
309d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
310d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
311d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (hadError) {
312d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // Do nothing
313d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    } else if (Init < NumInits) {
314d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      ILE->setInit(Init, MemberInit.takeAs<Expr>());
315d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    } else if (InitSeq.getKind()
316d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                 == InitializationSequence::ConstructorInitialization) {
317d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // Value-initialization requires a constructor call, so
318d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // extend the initializer list to include the constructor
319d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // call and make a note that we'll need to take another pass
320d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // through the initializer list.
321d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      ILE->updateInit(Init, MemberInit.takeAs<Expr>());
322d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      RequiresSecondPass = true;
323d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
324d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  } else if (InitListExpr *InnerILE
325d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor               = dyn_cast<InitListExpr>(ILE->getInit(Init)))
326d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    FillInValueInitializations(MemberEntity, InnerILE,
327d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                               RequiresSecondPass);
328d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor}
329d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
3304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Recursively replaces NULL values within the given initializer list
3314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// with expressions that perform value-initialization of the
3324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// appropriate type.
333cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregorvoid
334cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas GregorInitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
335cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                            InitListExpr *ILE,
336cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                            bool &RequiresSecondPass) {
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((ILE->getType() != SemaRef.Context.VoidTy) &&
338930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor         "Should not have void type");
33987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  SourceLocation Loc = ILE->getSourceRange().getBegin();
34087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (ILE->getSyntacticForm())
34187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3436217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
344d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (RType->getDecl()->isUnion() &&
345d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        ILE->getInitializedFieldInUnion())
346d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
347d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                              Entity, ILE, RequiresSecondPass);
348d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    else {
349d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      unsigned Init = 0;
350d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      for (RecordDecl::field_iterator
351d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor             Field = RType->getDecl()->field_begin(),
352d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor             FieldEnd = RType->getDecl()->field_end();
353d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor           Field != FieldEnd; ++Field) {
354d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (Field->isUnnamedBitfield())
355d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor          continue;
356d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
357d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (hadError)
35887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          return;
35987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
360d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
361d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (hadError)
362cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor          return;
363cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
364d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        ++Init;
365d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
366d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        // Only look at the first initialization of a union.
367d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (RType->getDecl()->isUnion())
368d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor          break;
369d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      }
3704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    }
3714c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3744c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3754c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  QualType ElementType;
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
377cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitializedEntity ElementEntity = Entity;
37887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumInits = ILE->getNumInits();
37987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumElements = NumInits;
3800820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
3814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = AType->getElementType();
38287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
38387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      NumElements = CAType->getSize().getZExtValue();
384cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
385cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                                         0, Entity);
386183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
3874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = VType->getElementType();
38887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    NumElements = VType->getNumElements();
389cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
390cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                                         0, Entity);
3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
3924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = ILE->getType();
3931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
394cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
39587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  for (unsigned Init = 0; Init != NumElements; ++Init) {
39616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor    if (hadError)
39716006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      return;
39816006c901315fa12a108b4e571f187f4b676e426Douglas Gregor
399d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
400d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson        ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
401cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      ElementEntity.setElementIndex(Init);
402cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
40387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (Init >= NumInits || !ILE->getInit(Init)) {
404cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
405cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                                                true);
406cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
407cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (!InitSeq) {
408cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
40987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        hadError = true;
41087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        return;
41187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      }
41287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
413cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      Sema::OwningExprResult ElementInit
414cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        = InitSeq.Perform(SemaRef, ElementEntity, Kind,
415cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                          Sema::MultiExprArg(SemaRef, 0, 0));
416cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (ElementInit.isInvalid()) {
41716006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        hadError = true;
418cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        return;
419cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      }
420cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
421cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (hadError) {
422cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        // Do nothing
423cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      } else if (Init < NumInits) {
424cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        ILE->setInit(Init, ElementInit.takeAs<Expr>());
425cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      } else if (InitSeq.getKind()
426cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                   == InitializationSequence::ConstructorInitialization) {
427cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        // Value-initialization requires a constructor call, so
428cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        // extend the initializer list to include the constructor
429cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        // call and make a note that we'll need to take another pass
430cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        // through the initializer list.
431cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        ILE->updateInit(Init, ElementInit.takeAs<Expr>());
432cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        RequiresSecondPass = true;
433cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      }
434ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump    } else if (InitListExpr *InnerILE
435cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
436cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
4374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
4384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
4394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
44068355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
441cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas GregorInitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
442cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                 InitListExpr *IL, QualType &T)
4430820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  : SemaRef(S) {
4440cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  hadError = false;
445c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
446b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  unsigned newIndex = 0;
4474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned newStructuredIndex = 0;
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FullyStructuredList
449ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
450eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
451eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        /*TopLevelObject=*/true);
452c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
453cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  if (!hadError) {
454cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    bool RequiresSecondPass = false;
455cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
45616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor    if (RequiresSecondPass && !hadError)
457cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      FillInValueInitializations(Entity, FullyStructuredList,
458cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                 RequiresSecondPass);
459cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  }
4600cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4610cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4620cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numArrayElements(QualType DeclType) {
463638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  // FIXME: use a proper constant
464638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  int maxElements = 0x7FFFFFFF;
465c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT =
4660820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
4670cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = static_cast<int>(CAT->getSize().getZExtValue());
4680cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
4690cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  return maxElements;
4700cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4710cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4720cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numStructUnionElements(QualType DeclType) {
4736217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
4744c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int InitializableMembers = 0;
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (RecordDecl::field_iterator
47617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         Field = structDecl->field_begin(),
47717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         FieldEnd = structDecl->field_end();
4784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor       Field != FieldEnd; ++Field) {
4794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if ((*Field)->getIdentifier() || !(*Field)->isBitField())
4804c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++InitializableMembers;
4814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
48239ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis  if (structDecl->isUnion())
483f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman    return std::min(InitializableMembers, 1);
484f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman  return InitializableMembers - structDecl->hasFlexibleArrayMember();
4850cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4860cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
4884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType T, unsigned &Index,
4894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
490eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
491eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
4920cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  int maxElements = 0;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4940cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (T->isArrayType())
4950cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numArrayElements(T);
4960cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else if (T->isStructureType() || T->isUnionType())
4970cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numStructUnionElements(T);
498b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  else if (T->isVectorType())
499183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    maxElements = T->getAs<VectorType>()->getNumElements();
5000cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else
5010cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    assert(0 && "CheckImplicitInitList(): Illegal type");
502b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
503402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  if (maxElements == 0) {
5040820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
505402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman                  diag::err_implicit_empty_initializer);
5064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
507402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    hadError = true;
508402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    return;
509402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  }
510402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman
5114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Build a structured initializer list corresponding to this subobject.
5124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitListExpr *StructuredSubobjectInitList
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 StructuredIndex,
515ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor          SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
516ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                      ParentIList->getSourceRange().getEnd()));
5174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned StructuredSubobjectInitIndex = 0;
5184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
5194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Check the element types and build the structural subobject.
52087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned StartIndex = Index;
5214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  CheckListElementTypes(ParentIList, T, false, Index,
5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                        StructuredSubobjectInitList,
523eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        StructuredSubobjectInitIndex,
524eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        TopLevelObject);
52587fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
526a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  StructuredSubobjectInitList->setType(T);
527a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor
528ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor  // Update the structured sub-object initializer so that it's ending
52987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  // range corresponds with the end of the last initializer it used.
53087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (EndIndex < ParentIList->getNumInits()) {
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SourceLocation EndLoc
53287fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
53387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    StructuredSubobjectInitList->setRBraceLoc(EndLoc);
53487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  }
5350cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5360cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
537a647caad2dec67ac25b763f06237cfe3c3968b51Steve Naroffvoid InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
5384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
5394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
540eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
541eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
542c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
5434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  SyntacticToSemantic[IList] = StructuredList;
5444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList->setSyntacticForm(IList);
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CheckListElementTypes(IList, T, true, Index, StructuredList,
546eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        StructuredIndex, TopLevelObject);
547a647caad2dec67ac25b763f06237cfe3c3968b51Steve Naroff  IList->setType(T);
5484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList->setType(T);
549638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (hadError)
550638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
551c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
552638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (Index < IList->getNumInits()) {
553c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    // We have leftover initializers
554e540858b289b23653bcb23646f135729203635cbEli Friedman    if (StructuredIndex == 1 &&
555e540858b289b23653bcb23646f135729203635cbEli Friedman        IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
5567c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
557e540858b289b23653bcb23646f135729203635cbEli Friedman      if (SemaRef.getLangOptions().CPlusPlus) {
5587c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor        DK = diag::err_excess_initializers_in_char_array_initializer;
559e540858b289b23653bcb23646f135729203635cbEli Friedman        hadError = true;
560e540858b289b23653bcb23646f135729203635cbEli Friedman      }
561bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      // Special-case
5620820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
563dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getInit(Index)->getSourceRange();
564d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    } else if (!T->isIncompleteType()) {
565b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // Don't complain for incomplete types, since we'll get an error
566b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // elsewhere
567eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType CurrentObjectType = StructuredList->getType();
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      int initKind =
569eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isArrayType()? 0 :
570eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isVectorType()? 1 :
571eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isScalarType()? 2 :
572eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isUnionType()? 3 :
573eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        4;
5747c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
5757c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers;
576e540858b289b23653bcb23646f135729203635cbEli Friedman      if (SemaRef.getLangOptions().CPlusPlus) {
577e540858b289b23653bcb23646f135729203635cbEli Friedman        DK = diag::err_excess_initializers;
5780863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman        hadError = true;
5790863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman      }
5800863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman      if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
5810863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman        DK = diag::err_excess_initializers;
582e540858b289b23653bcb23646f135729203635cbEli Friedman        hadError = true;
583e540858b289b23653bcb23646f135729203635cbEli Friedman      }
5847c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
5850820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
586eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        << initKind << IList->getInit(Index)->getSourceRange();
587c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    }
588c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  }
589cda25a977e4b7fe4e080b87586410eaeab7b62f6Eli Friedman
590759f25237864f3a3cc23eb01f0c0ce6edcc9342dEli Friedman  if (T->isScalarType() && !TopLevelObject)
5910820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
592a3a835149ed4b183e3b009a1f94a6123779d696bDouglas Gregor      << IList->getSourceRange()
59329d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner      << CodeModificationHint::CreateRemoval(IList->getLocStart())
59429d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner      << CodeModificationHint::CreateRemoval(IList->getLocEnd());
5950cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5960cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
597b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedmanvoid InitListChecker::CheckListElementTypes(InitListExpr *IList,
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            QualType &DeclType,
59987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            bool SubobjectIsDesignatorContext,
6004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
6014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
602eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
603eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
604c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (DeclType->isScalarType()) {
6056fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
606c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (DeclType->isVectorType()) {
6074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
608d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor  } else if (DeclType->isAggregateType()) {
609d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor    if (DeclType->isRecordType()) {
6106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
6124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                            SubobjectIsDesignatorContext, Index,
613eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                            StructuredList, StructuredIndex,
614eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                            TopLevelObject);
61587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    } else if (DeclType->isArrayType()) {
616f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      llvm::APSInt Zero(
6170820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner                      SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
618f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor                      false);
6194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
6204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                     StructuredList, StructuredIndex);
621ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump    } else
6224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      assert(0 && "Aggregate that isn't a structure or array?!");
623613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff  } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
624613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff    // This type is invalid, issue a diagnostic.
6254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
6260820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
627d162584991885ab004a02573a73ce06422b921fcChris Lattner      << DeclType;
628d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    hadError = true;
629930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isRecordType()) {
630930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init]p14:
631930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   [...] If the class is an aggregate (8.5.1), and the initializer
632930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   is a brace-enclosed list, see 8.5.1.
633930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //
634930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Note: 8.5.1 is handled below; here, we diagnose the case where
635930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // we have an initializer list and a destination type that is not
636930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // an aggregate.
637930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // FIXME: In C++0x, this is yet another form of initialization.
6380820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
639930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << DeclType << IList->getSourceRange();
640930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
641930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isReferenceType()) {
642930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
6430cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  } else {
6440cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // In C, all types are either scalars or aggregates, but
6451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // additional handling is needed here for C++ (and possibly others?).
6460cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    assert(0 && "Unsupported initializer type");
6470cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
6480cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
6490cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
650b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedmanvoid InitListChecker::CheckSubElementType(InitListExpr *IList,
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          QualType ElemType,
6524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &Index,
6534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          InitListExpr *StructuredList,
6544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &StructuredIndex) {
6556fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor  Expr *expr = IList->getInit(Index);
656c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
657c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    unsigned newIndex = 0;
6584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned newStructuredIndex = 0;
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    InitListExpr *newStructuredList
6604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      = getStructuredSubobjectInit(IList, Index, ElemType,
6614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                   StructuredList, StructuredIndex,
6624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                   SubInitList->getSourceRange());
6631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CheckExplicitInitList(SubInitList, ElemType, newIndex,
6644c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          newStructuredList, newStructuredIndex);
6654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
6664c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
66779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
66879e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    CheckStringInit(Str, ElemType, SemaRef);
669f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner    UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
6704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
671c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (ElemType->isScalarType()) {
6726fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
673930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (ElemType->isReferenceType()) {
674930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
675b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  } else {
6760820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    if (SemaRef.getLangOptions().CPlusPlus) {
677930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // C++ [dcl.init.aggr]p12:
678930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   All implicit type conversions (clause 4) are considered when
679930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   initializing the aggregate member with an ini- tializer from
680930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   an initializer-list. If the initializer can initialize a
681930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   member, the member is initialized. [...]
6821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ImplicitConversionSequence ICS
683d28b42862bc627f4fc1430b4a1919b304800dc1cAnders Carlsson        = SemaRef.TryCopyInitialization(expr, ElemType,
684d28b42862bc627f4fc1430b4a1919b304800dc1cAnders Carlsson                                        /*SuppressUserConversions=*/false,
6857b361b588031483658c4364e02026ffb06e78c26Anders Carlsson                                        /*ForceRValue=*/false,
6867b361b588031483658c4364e02026ffb06e78c26Anders Carlsson                                        /*InOverloadResolution=*/false);
687d28b42862bc627f4fc1430b4a1919b304800dc1cAnders Carlsson
6881d31833450e6d2947a33cb0840d87661d92eec07John McCall      if (!ICS.isBad()) {
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
6906864748fc9a780e6db0bb5a7bd20aa889882dc94Douglas Gregor                                              Sema::AA_Initializing))
691930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor          hadError = true;
692930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
693930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        ++Index;
694930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        return;
695930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      }
696930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
697930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Fall through for subaggregate initialization
698930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    } else {
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // C99 6.7.8p13:
700930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //
701930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   The initializer for a structure or union object that has
702930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   automatic storage duration shall be either an initializer
703930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   list as described below, or a single expression that has
704930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   compatible structure or union type. In the latter case, the
705930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   initial value of the object, including unnamed members, is
706930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      //   that of the expression.
7076b5374f837f925d99b1a76bc2fe8c98c1698df7fEli Friedman      if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
7088718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman          SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
709930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
710930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        ++Index;
711930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        return;
712930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      }
713930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
714930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // Fall through for subaggregate initialization
715930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
716930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
717930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init.aggr]p12:
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
719930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   [...] Otherwise, if the member is itself a non-empty
720930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   subaggregate, brace elision is assumed and the initializer is
721930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   considered for the initialization of the first member of
722930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    //   the subaggregate.
723930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (ElemType->isAggregateType() || ElemType->isVectorType()) {
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      CheckImplicitInitList(IList, ElemType, Index, StructuredList,
725930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                            StructuredIndex);
726930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
727930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    } else {
728930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // We cannot initialize this element, so let
729930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // PerformCopyInitialization produce the appropriate diagnostic.
7306864748fc9a780e6db0bb5a7bd20aa889882dc94Douglas Gregor      SemaRef.PerformCopyInitialization(expr, ElemType, Sema::AA_Initializing);
731930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
732930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
733930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
734930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
735930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
736b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman}
737b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
738930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
7396fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor                                      unsigned &Index,
7404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
7414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
7420cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
7436fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    Expr *expr = IList->getInit(Index);
744c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    if (isa<InitListExpr>(expr)) {
7450820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
746dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                    diag::err_many_braces_around_scalar_init)
747dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getSourceRange();
748bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      hadError = true;
749bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      ++Index;
7504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
751bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      return;
75205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    } else if (isa<DesignatedInitExpr>(expr)) {
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.Diag(expr->getSourceRange().getBegin(),
75405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                    diag::err_designator_for_scalar_init)
75505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        << DeclType << expr->getSourceRange();
75605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      hadError = true;
75705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Index;
7584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
75905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      return;
7600cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
76105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
762c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    Sema::OwningExprResult Result =
763c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      CheckSingleInitializer(0, SemaRef.Owned(expr), DeclType, SemaRef);
764c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
765c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    Expr *ResultExpr;
766c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
767c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    if (Result.isInvalid())
768bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      hadError = true; // types weren't compatible.
769c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson    else {
770c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      ResultExpr = Result.takeAs<Expr>();
771c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson
772c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      if (ResultExpr != expr) {
773c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson        // The type was promoted, update initializer list.
774c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson        IList->setInit(Index, ResultExpr);
775c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      }
77605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
7774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (hadError)
7784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++StructuredIndex;
7794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    else
780c07b8c02f7c54631ab9a9bd7db9f031d3db170caAnders Carlsson      UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
7810cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    ++Index;
782bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman  } else {
7830820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
784dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << IList->getSourceRange();
785bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    hadError = true;
7864c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
7874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
788bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    return;
7890cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
7900cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
7910cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
792930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorvoid InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
793930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &Index,
794930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         InitListExpr *StructuredList,
795930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &StructuredIndex) {
796930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  if (Index < IList->getNumInits()) {
797930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    Expr *expr = IList->getInit(Index);
798930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (isa<InitListExpr>(expr)) {
7990820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
800930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor        << DeclType << IList->getSourceRange();
801930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
802930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
803930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
804930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      return;
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
806930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
807930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
8082de3aced4c4373d8a078604c8e61e267a323853aAnders Carlsson    if (SemaRef.CheckReferenceInit(expr, DeclType,
809739d8283149d999f598a7514c6ec2b42598f51d3Douglas Gregor                                   /*FIXME:*/expr->getLocStart(),
8102de3aced4c4373d8a078604c8e61e267a323853aAnders Carlsson                                   /*SuppressUserConversions=*/false,
8112de3aced4c4373d8a078604c8e61e267a323853aAnders Carlsson                                   /*AllowExplicit=*/false,
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                   /*ForceRValue=*/false))
813930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      hadError = true;
814930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    else if (savExpr != expr) {
815930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // The type was promoted, update initializer list.
816930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      IList->setInit(Index, expr);
817930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
818930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    if (hadError)
819930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++StructuredIndex;
820930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    else
821930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
822930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
823930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else {
824390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: It would be wonderful if we could point at the actual member. In
825390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // general, it would be useful to pass location information down the stack,
826390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // so that we know the location (or decl) of the "current object" being
827390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // initialized.
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(IList->getLocStart(),
829930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                  diag::err_init_reference_member_uninitialized)
830930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << DeclType
831930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      << IList->getSourceRange();
832930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
833930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
834930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++StructuredIndex;
835930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    return;
836930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
837930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor}
838930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
8404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
8414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
8424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
8430cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
844183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const VectorType *VT = DeclType->getAs<VectorType>();
8452ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    unsigned maxElements = VT->getNumElements();
8462ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    unsigned numEltsInit = 0;
8470cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    QualType elementType = VT->getElementType();
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8492ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    if (!SemaRef.getLangOptions().OpenCL) {
8502ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
8512ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        // Don't attempt to go past the end of the init list
8522ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        if (Index >= IList->getNumInits())
8532ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          break;
8542ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        CheckSubElementType(IList, elementType, Index,
8552ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                            StructuredList, StructuredIndex);
8562ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      }
8572ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    } else {
8582ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      // OpenCL initializers allows vectors to be constructed from vectors.
8592ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      for (unsigned i = 0; i < maxElements; ++i) {
8602ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        // Don't attempt to go past the end of the init list
8612ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        if (Index >= IList->getNumInits())
8622ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          break;
8632ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        QualType IType = IList->getInit(Index)->getType();
8642ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        if (!IType->isVectorType()) {
8652ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          CheckSubElementType(IList, elementType, Index,
8662ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                              StructuredList, StructuredIndex);
8672ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          ++numEltsInit;
8682ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        } else {
869183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall          const VectorType *IVT = IType->getAs<VectorType>();
8702ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          unsigned numIElts = IVT->getNumElements();
8712ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          QualType VecType = SemaRef.Context.getExtVectorType(elementType,
8722ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                                              numIElts);
8732ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          CheckSubElementType(IList, VecType, Index,
8742ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                              StructuredList, StructuredIndex);
8752ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          numEltsInit += numIElts;
8762ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        }
8772ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      }
8780cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8802ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    // OpenCL & AltiVec require all elements to be initialized.
8812ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    if (numEltsInit != maxElements)
8822ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
8832ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        SemaRef.Diag(IList->getSourceRange().getBegin(),
8842ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                     diag::err_vector_incorrect_num_initializers)
8852ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman          << (numEltsInit < maxElements) << maxElements << numEltsInit;
8860cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
8870cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
8880cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
8891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
89087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                     llvm::APSInt elementIndex,
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     bool SubobjectIsDesignatorContext,
8924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &Index,
8934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     InitListExpr *StructuredList,
8944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &StructuredIndex) {
8950cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  // Check for the special-case of initializing an array with a string.
8960cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
89779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
89879e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner                                 SemaRef.Context)) {
89979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner      CheckStringInit(Str, DeclType, SemaRef);
9004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // We place the string literal directly into the resulting
9014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // initializer list. This is the only place where the structure
9024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // of the structured initializer list doesn't match exactly,
9034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // because doing so would involve allocating one character
9044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // constant for each string.
905f71ae8d8024561f92dd7916363e7a791684563ccChris Lattner      UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
9060820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
9070cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      ++Index;
9080cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      return;
9090cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
9100cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
911c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const VariableArrayType *VAT =
9120820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsVariableArrayType(DeclType)) {
913638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // Check for VLAs; in standard C it would be possible to check this
914638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // earlier, but I don't know where clang accepts VLAs (gcc accepts
915638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // them in all sorts of strange places).
9160820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
917dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_variable_object_no_init)
918dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << VAT->getSizeExpr()->getSourceRange();
919638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    hadError = true;
9204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
9214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
922638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
923638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  }
924638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman
92505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // We might know the maximum number of elements in advance.
9264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  llvm::APSInt maxElements(elementIndex.getBitWidth(),
9274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           elementIndex.isUnsigned());
92805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool maxElementsKnown = false;
92905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (const ConstantArrayType *CAT =
9300820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
93105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElements = CAT->getSize();
932f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor    elementIndex.extOrTrunc(maxElements.getBitWidth());
933e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    elementIndex.setIsUnsigned(maxElements.isUnsigned());
93405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElementsKnown = true;
93505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
93605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
9370820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
938c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                             ->getElementType();
93905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
94005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
94105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
94287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
94387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
94487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
94587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
94687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
94787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
94887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. elementIndex will be
94987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // updated to be the next array element we'll initialize.
9501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (CheckDesignatedInitializer(IList, DIE, 0,
9514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, 0, &elementIndex, Index,
952eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex, true,
953eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     false)) {
95405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
95587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        continue;
95687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      }
95787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
958f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      if (elementIndex.getBitWidth() > maxElements.getBitWidth())
959f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor        maxElements.extend(elementIndex.getBitWidth());
960f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
961f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor        elementIndex.extend(maxElements.getBitWidth());
962e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor      elementIndex.setIsUnsigned(maxElements.isUnsigned());
963f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor
96487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If the array is of incomplete type, keep track of the number of
96587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // elements in the initializer.
96687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!maxElementsKnown && elementIndex > maxElements)
96787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        maxElements = elementIndex;
96805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
96905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
97005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
97105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
97205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we know the maximum number of elements, and we've already
97305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // hit it, stop consuming elements in the initializer list.
97405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElementsKnown && elementIndex == maxElements)
9750cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      break;
97605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
97705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // Check this element.
9786fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, elementType, Index,
9794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
98005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++elementIndex;
98105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
98205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If the array is of incomplete type, keep track of the number of
98305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // elements in the initializer.
98405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (!maxElementsKnown && elementIndex > maxElements)
98505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      maxElements = elementIndex;
9860cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
987587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman  if (!hadError && DeclType->isIncompleteArrayType()) {
9880cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // If this is an incomplete array type, the actual type needs to
989396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar    // be calculated here.
990e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
99105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElements == Zero) {
992396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // Sizing an array implicitly to zero is not allowed by ISO C,
993396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // but is supported by GNU.
9940820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
995396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                    diag::ext_typecheck_zero_array_size);
9960cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
997396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
999396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                                                     ArrayType::Normal, 0);
10000cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
10010cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
10020cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            QualType DeclType,
100587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            RecordDecl::field_iterator Field,
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            bool SubobjectIsDesignatorContext,
10074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
10084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
1009eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
1010eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
10116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
10121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1013b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // If the record is invalid, some of it's members are invalid. To avoid
1014b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // confusion, we forgo checking the intializer for the entire record.
1015b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  if (structDecl->isInvalidDecl()) {
1016b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    hadError = true;
1017b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    return;
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
10193498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
10203498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  if (DeclType->isUnionType() && IList->getNumInits() == 0) {
10213498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    // Value-initialize the first named member of the union.
10226217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
102317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    for (RecordDecl::field_iterator FieldEnd = RD->field_end();
10243498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor         Field != FieldEnd; ++Field) {
10253498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      if (Field->getDeclName()) {
10263498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor        StructuredList->setInitializedFieldInUnion(*Field);
10273498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor        break;
10283498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      }
10293498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    }
10303498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return;
10313498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  }
10323498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
103305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // If structDecl is a forward declaration, this loop won't do
103405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // anything except look at designated initializers; That's okay,
103505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // because an error should get printed out elsewhere. It might be
103605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // worthwhile to skip over the rest of the initializer, though.
10376217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
103817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  RecordDecl::field_iterator FieldEnd = RD->field_end();
1039dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor  bool InitializedSomething = false;
104005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
104105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
104205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
104305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
104487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
104587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
104687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
104787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
104887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
104987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
105087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. Field will be updated to
105187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the next field that we'll be initializing.
10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (CheckDesignatedInitializer(IList, DIE, 0,
10534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, &Field, 0, Index,
1054eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex,
1055eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, TopLevelObject))
105605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
105705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1058dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      InitializedSomething = true;
105905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
106005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
106105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
106205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field == FieldEnd) {
106305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      // We've run out of fields. We're done.
106444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      break;
106505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
106644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1067dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    // We've already initialized a member of a union. We're done.
1068dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    if (InitializedSomething && DeclType->isUnionType())
1069dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      break;
1070dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor
107105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we've hit the flexible array member at the end, we're done.
107205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field->getType()->isIncompleteArrayType())
1073b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      break;
107444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10750bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (Field->isUnnamedBitfield()) {
10764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // Don't initialize unnamed bitfields, e.g. "int : 20;"
107705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Field;
1078b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      continue;
10790cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
108044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10816fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, Field->getType(), Index,
10824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
1083dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    InitializedSomething = true;
10840bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
10850bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (DeclType->isUnionType()) {
10860bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Initialize the first field within the union.
10870bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      StructuredList->setInitializedFieldInUnion(*Field);
10880bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
108905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
109005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++Field;
10910cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
109244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
1094a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      Index >= IList->getNumInits())
1095eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    return;
1096eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1097eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  // Handle GNU flexible array initializers.
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!TopLevelObject &&
1099a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      (!isa<InitListExpr>(IList->getInit(Index)) ||
1100a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor       cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1102eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                  diag::err_flexible_array_init_nonempty)
1103eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      << IList->getInit(Index)->getSourceRange().getBegin();
11040820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1105eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      << *Field;
1106eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    hadError = true;
1107a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    ++Index;
1108a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    return;
1109a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  } else {
11101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1111a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                 diag::ext_flexible_array_init)
1112a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      << IList->getInit(Index)->getSourceRange().getBegin();
1113a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1114a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      << *Field;
1115eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  }
1116eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1117a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  if (isa<InitListExpr>(IList->getInit(Index)))
1118a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1119a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                        StructuredIndex);
1120a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor  else
1121a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1122a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor                          StructuredIndex);
11230cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
11240cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
1125ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Expand a field designator that refers to a member of an
1126ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// anonymous struct or union into a series of field designators that
1127ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// refers to the field within the appropriate subobject.
1128ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor///
1129ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// Field/FieldIndex will be updated to point to the (new)
1130ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// currently-designated field.
1131ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregorstatic void ExpandAnonymousFieldDesignator(Sema &SemaRef,
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           DesignatedInitExpr *DIE,
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           unsigned DesigIdx,
1134ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                           FieldDecl *Field,
1135ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                        RecordDecl::field_iterator &FieldIter,
1136ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                           unsigned &FieldIndex) {
1137ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  typedef DesignatedInitExpr::Designator Designator;
1138ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1139ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Build the path from the current object to the member of the
1140ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // anonymous struct/union (backwards).
1141ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  llvm::SmallVector<FieldDecl *, 4> Path;
1142ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1144ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Build the replacement designators.
1145ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  llvm::SmallVector<Designator, 4> Replacements;
1146ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1147ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor         FI = Path.rbegin(), FIEnd = Path.rend();
1148ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor       FI != FIEnd; ++FI) {
1149ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (FI + 1 == FIEnd)
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Replacements.push_back(Designator((IdentifierInfo *)0,
1151ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                    DIE->getDesignator(DesigIdx)->getDotLoc(),
1152ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                DIE->getDesignator(DesigIdx)->getFieldLoc()));
1153ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    else
1154ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1155ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                        SourceLocation()));
1156ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Replacements.back().setField(*FI);
1157ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
1158ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1159ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Expand the current designator into the set of replacement
1160ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // designators, so we have a full subobject path down to where the
1161ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // member of the anonymous struct/union is actually stored.
1162319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
1163ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                        &Replacements[0] + Replacements.size());
11641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1165ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Update FieldIter/FieldIndex;
1166ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
116717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  FieldIter = Record->field_begin();
1168ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  FieldIndex = 0;
116917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator FEnd = Record->field_end();
1170ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor       FieldIter != FEnd; ++FieldIter) {
1171ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (FieldIter->isUnnamedBitfield())
1172ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor        continue;
1173ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1174ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (*FieldIter == Path.back())
1175ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      return;
1176ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1177ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    ++FieldIndex;
1178ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
1179ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1180ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  assert(false && "Unable to find anonymous struct/union field");
1181ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
1182ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
118305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @brief Check the well-formedness of a C99 designated initializer.
118405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
118505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Determines whether the designated initializer @p DIE, which
118605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// resides at the given @p Index within the initializer list @p
118705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// IList, is well-formed for a current object of type @p DeclType
118805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// (C99 6.7.8). The actual subobject that this designator refers to
11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// within the current subobject is returned in either
11904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @p NextField or @p NextElementIndex (whichever is appropriate).
119105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
119205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param IList  The initializer list in which this designated
119305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// initializer occurs.
119405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
1195711997184366d584c9c437102cae1e9d9927b986Douglas Gregor/// @param DIE The designated initializer expression.
1196711997184366d584c9c437102cae1e9d9927b986Douglas Gregor///
1197711997184366d584c9c437102cae1e9d9927b986Douglas Gregor/// @param DesigIdx  The index of the current designator.
119805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
119905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param DeclType  The type of the "current object" (C99 6.7.8p17),
120005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// into which the designation in @p DIE should refer.
120105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
120287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextField  If non-NULL and the first designator in @p DIE is
120387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// a field, this will be set to the field declaration corresponding
120487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// to the field named by the designator.
120505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
120687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextElementIndex  If non-NULL and the first designator in @p
120787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// DIE is an array designator or GNU array-range designator, this
120887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// will be set to the last index initialized by this designator.
120905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
121005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param Index  Index into @p IList where the designated initializer
121105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @p DIE occurs.
121205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
12134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @param StructuredList  The initializer list expression that
12144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// describes all of the subobject initializers in the order they'll
12154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// actually be initialized.
12164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor///
121705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @returns true if there was an error, false otherwise.
12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
121987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas GregorInitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                      DesignatedInitExpr *DIE,
1221711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                      unsigned DesigIdx,
122287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      QualType &CurrentObjectType,
122387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      RecordDecl::field_iterator *NextField,
122487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                      llvm::APSInt *NextElementIndex,
12254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
12264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
122734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                      unsigned &StructuredIndex,
1228eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool FinishSubobjectInit,
1229eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
1230711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  if (DesigIdx == DIE->size()) {
123187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the actual initialization for the designated object type.
123287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
12336fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
12346fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Temporarily remove the designator expression from the
12356fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // initializer list that the child calls see, so that we don't try
12366fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // to re-process the designator.
12376fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    unsigned OldIndex = Index;
12386fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE->getInit());
12396fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
12406fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    CheckSubElementType(IList, CurrentObjectType, Index,
12414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
12426fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
12436fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Restore the designated initializer expression in the syntactic
12446fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // form of the initializer list.
12456fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    if (IList->getInit(OldIndex) != DIE->getInit())
12466fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      DIE->setInit(IList->getInit(OldIndex));
12476fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE);
12486fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
124987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
125087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
125105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1252711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  bool IsFirstDesignator = (DesigIdx == 0);
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((IsFirstDesignator || StructuredList) &&
12544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor         "Need a non-designated initializer list to start from");
12554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1256711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
12574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Determine the structural initializer list that corresponds to the
12584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // current subobject.
12594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
12601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
1261ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                                 StructuredList, StructuredIndex,
12624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                 SourceRange(D->getStartLocation(),
12634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                             DIE->getSourceRange().getEnd()));
12644c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  assert(StructuredList && "Expected a structured initializer list");
12654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
126687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (D->isFieldDesignator()) {
126787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // C99 6.7.8p7:
126887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
126987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   If a designator has the form
127087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
127187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //      . identifier
127287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
127387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   then the current object (defined below) shall have
127487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   structure or union type and the identifier shall be the
12751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   name of a member of that type.
12766217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const RecordType *RT = CurrentObjectType->getAs<RecordType>();
127787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (!RT) {
127887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      SourceLocation Loc = D->getDotLoc();
127987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (Loc.isInvalid())
128087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        Loc = D->getFieldLoc();
12810820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
12820820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
128387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
128487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
128587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
128687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
12874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Note: we perform a linear search of the fields here, despite
12884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the fact that we have a faster lookup method, because we always
12894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // need to compute the field's index.
1290ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    FieldDecl *KnownField = D->getField();
129187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IdentifierInfo *FieldName = D->getFieldName();
12924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned FieldIndex = 0;
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    RecordDecl::field_iterator
129417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Field = RT->getDecl()->field_begin(),
129517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      FieldEnd = RT->getDecl()->field_end();
12964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    for (; Field != FieldEnd; ++Field) {
12974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->isUnnamedBitfield())
12984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        continue;
12994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1300ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      if (KnownField == *Field || Field->getIdentifier() == FieldName)
13014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        break;
13024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
13034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++FieldIndex;
130487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
130587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
13064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (Field == FieldEnd) {
1307ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // There was no normal field in the struct with the designated
1308ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // name. Perform another lookup for this name, which may find
1309ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // something that we can't designate (e.g., a member function),
1310ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // may find nothing, or may find a member of an anonymous
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // struct/union.
131217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1313c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      FieldDecl *ReplacementField = 0;
13144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Lookup.first == Lookup.second) {
1315c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // Name lookup didn't find anything. Determine whether this
1316c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // was a typo for another field name.
1317c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        LookupResult R(SemaRef, FieldName, D->getFieldLoc(),
1318c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                       Sema::LookupMemberName);
1319c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl()) &&
1320c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            (ReplacementField = R.getAsSingle<FieldDecl>()) &&
1321c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            ReplacementField->getDeclContext()->getLookupContext()
1322c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                                                      ->Equals(RT->getDecl())) {
1323c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          SemaRef.Diag(D->getFieldLoc(),
1324c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                       diag::err_field_designator_unknown_suggest)
1325c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            << FieldName << CurrentObjectType << R.getLookupName()
1326c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            << CodeModificationHint::CreateReplacement(D->getFieldLoc(),
1327c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                                               R.getLookupName().getAsString());
132867dd1d4df1b28973e12e0981129b2517d2033b66Douglas Gregor          SemaRef.Diag(ReplacementField->getLocation(),
132967dd1d4df1b28973e12e0981129b2517d2033b66Douglas Gregor                       diag::note_previous_decl)
133067dd1d4df1b28973e12e0981129b2517d2033b66Douglas Gregor            << ReplacementField->getDeclName();
1331c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        } else {
1332c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1333c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            << FieldName << CurrentObjectType;
1334c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          ++Index;
1335c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          return true;
1336c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        }
1337c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      } else if (!KnownField) {
1338c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // Determine whether we found a field at all.
1339c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        ReplacementField = dyn_cast<FieldDecl>(*Lookup.first);
1340c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      }
1341c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1342c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      if (!ReplacementField) {
13434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        // Name lookup found something, but it wasn't a field.
13440820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
13454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor          << FieldName;
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        SemaRef.Diag((*Lookup.first)->getLocation(),
13474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                      diag::note_field_designator_found);
1348ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        ++Index;
1349ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        return true;
1350ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      }
1351c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1352c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      if (!KnownField &&
1353c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          cast<RecordDecl>((ReplacementField)->getDeclContext())
1354c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                                                 ->isAnonymousStructOrUnion()) {
1355c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // Handle an field designator that refers to a member of an
1356c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // anonymous struct or union.
1357c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
1358c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                                       ReplacementField,
1359c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                                       Field, FieldIndex);
1360c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        D = DIE->getDesignator(DesigIdx);
1361c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      } else if (!KnownField) {
1362c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // The replacement field comes from typo correction; find it
1363c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // in the list of fields.
1364c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        FieldIndex = 0;
1365c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        Field = RT->getDecl()->field_begin();
1366c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        for (; Field != FieldEnd; ++Field) {
1367c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          if (Field->isUnnamedBitfield())
1368c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            continue;
1369c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1370c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          if (ReplacementField == *Field ||
1371c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor              Field->getIdentifier() == ReplacementField->getIdentifier())
1372c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            break;
1373c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1374c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          ++FieldIndex;
1375c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        }
1376c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      }
1377ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    } else if (!KnownField &&
1378ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor               cast<RecordDecl>((*Field)->getDeclContext())
13794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 ->isAnonymousStructOrUnion()) {
1380ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1381ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                     Field, FieldIndex);
1382ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      D = DIE->getDesignator(DesigIdx);
138387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
13844c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
13854c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // All of the fields of a union are located at the same place in
13864c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the initializer list.
13870bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (RT->getDecl()->isUnion()) {
13884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      FieldIndex = 0;
13890bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      StructuredList->setInitializedFieldInUnion(*Field);
13900bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
13914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
139287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Update the designator with the field declaration.
13934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    D->setField(*Field);
13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Make sure that our non-designated initializer list has space
13964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // for a subobject corresponding to this field.
13974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (FieldIndex >= StructuredList->getNumInits())
13980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
13994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1400eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    // This designator names a flexible array member.
1401eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    if (Field->getType()->isIncompleteArrayType()) {
1402eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool Invalid = false;
1403711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if ((DesigIdx + 1) != DIE->size()) {
1404eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // We can't designate an object within the flexible array
1405eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // member (because GCC doesn't allow it).
14061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        DesignatedInitExpr::Designator *NextD
1407711997184366d584c9c437102cae1e9d9927b986Douglas Gregor          = DIE->getDesignator(DesigIdx + 1);
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        SemaRef.Diag(NextD->getStartLocation(),
1409eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_designator_into_flexible_array_member)
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          << SourceRange(NextD->getStartLocation(),
1411eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                         DIE->getSourceRange().getEnd());
14120820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1413eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1414eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1415eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1416eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1417eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1418eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // The initializer is not an initializer list.
14190820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
1420eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_flexible_array_init_needs_braces)
1421eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << DIE->getInit()->getSourceRange();
14220820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1423eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1424eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1425eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1426eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1427eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Handle GNU flexible array initializers.
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (!Invalid && !TopLevelObject &&
1429eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
14301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        SemaRef.Diag(DIE->getSourceRange().getBegin(),
1431eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                      diag::err_flexible_array_init_nonempty)
1432eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << DIE->getSourceRange().getBegin();
14330820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1434eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          << *Field;
1435eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1436eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1437eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1438eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (Invalid) {
1439eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Index;
1440eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1441eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1442eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1443eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Initialize the array.
1444eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool prevHadError = hadError;
1445eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1446eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned OldIndex = Index;
1447eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(Index, DIE->getInit());
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      CheckSubElementType(IList, Field->getType(), Index,
1449eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                          StructuredList, newStructuredIndex);
1450eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(OldIndex, DIE);
1451eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (hadError && !prevHadError) {
1452eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Field;
1453eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++FieldIndex;
1454eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        if (NextField)
1455eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          *NextField = Field;
1456eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        StructuredIndex = FieldIndex;
1457eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1458eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1459eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    } else {
1460eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Recurse to check later designated subobjects.
1461eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType FieldType = (*Field)->getType();
1462eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1463711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
1464711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                     Index, StructuredList, newStructuredIndex,
1465eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, false))
1466eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1467eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    }
146887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
146987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Find the position of the next field to be initialized in this
147087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // subobject.
147187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Field;
14724c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++FieldIndex;
147305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
147487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // If this the first designator, our caller will continue checking
147587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // the rest of this struct/class/union subobject.
147687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (IsFirstDesignator) {
147787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (NextField)
147887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        *NextField = Field;
14794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      StructuredIndex = FieldIndex;
148087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return false;
148187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
148205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
148334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (!FinishSubobjectInit)
148434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return false;
148534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
1486ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    // We've already initialized something in the union; we're done.
1487ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (RT->getDecl()->isUnion())
1488ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      return hadError;
1489ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
149087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the remaining fields within this class/struct/union subobject.
149187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
14924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
14934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          StructuredList, FieldIndex);
149487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
149587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
149605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
149787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // C99 6.7.8p6:
149887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
149987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   If a designator has the form
150087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
150187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ]
150287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
150387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   then the current object (defined below) shall have array
150487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   type and the expression shall be an integer constant
150587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   expression. If the array is of unknown size, any
150687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   nonnegative value is valid.
150787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
150887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Additionally, cope with the GNU extension that permits
150987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // designators of the form
151087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
151187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ... constant-expression ]
15120820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
151387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (!AT) {
15140820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
151587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      << CurrentObjectType;
151687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Index;
151787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return true;
151887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
151905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
152087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  Expr *IndexExpr = 0;
152134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
152234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (D->isArrayDesignator()) {
152387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayIndex(*D);
15243bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
152534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex = DesignatedStartIndex;
152634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
152787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    assert(D->isArrayRangeDesignator() && "Need array-range designator");
15283bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    DesignatedStartIndex =
15313bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner      DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    DesignatedEndIndex =
15333bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner      DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
153487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayRangeEnd(*D);
153534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
15363bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
1537a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor      FullyStructuredList->sawArrayRangeDesignator();
153887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
153905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
154087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (isa<ConstantArrayType>(AT)) {
154187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
154234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
154334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
154434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
154534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
154634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedEndIndex >= MaxElements) {
15470820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
154887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                    diag::err_array_designator_too_large)
154934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor        << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
155087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        << IndexExpr->getSourceRange();
155187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
155287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
155305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
155434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
155534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Make sure the bit-widths and signedness match.
155634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
155734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
15583bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    else if (DesignatedStartIndex.getBitWidth() <
15593bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner             DesignatedEndIndex.getBitWidth())
156034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
156134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(true);
156234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(true);
156305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Make sure that our non-designated initializer list has space
15664c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // for a subobject corresponding to this array element.
156734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    StructuredList->resizeInits(SemaRef.Context,
156934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                DesignatedEndIndex.getZExtValue() + 1);
157034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
157134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Repeatedly perform subobject initializations in the range
157234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // [DesignatedStartIndex, DesignatedEndIndex].
157334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
157434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Move to the next designator
157534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
157634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned OldIndex = Index;
157734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  while (DesignatedStartIndex <= DesignatedEndIndex) {
157834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Recurse to check later designated subobjects.
157934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    QualType ElementType = AT->getElementType();
158034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    Index = OldIndex;
1581711997184366d584c9c437102cae1e9d9927b986Douglas Gregor    if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
1582711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                   Index, StructuredList, ElementIndex,
1583eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   (DesignatedStartIndex == DesignatedEndIndex),
1584eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   false))
158534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return true;
158687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
158734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Move to the next index in the array that we'll be initializing.
158834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ++DesignatedStartIndex;
158934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ElementIndex = DesignatedStartIndex.getZExtValue();
159034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  }
159187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
159287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // If this the first designator, our caller will continue checking
159387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // the rest of this array subobject.
159487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (IsFirstDesignator) {
159587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (NextElementIndex)
159634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      *NextElementIndex = DesignatedStartIndex;
15974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredIndex = ElementIndex;
159887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return false;
159987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
16001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
160134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (!FinishSubobjectInit)
160234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    return false;
160334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
160487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Check the remaining elements within this array subobject.
160505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool prevHadError = hadError;
1606fdf556936f94344d5482747403f27822cf0ae37fDouglas Gregor  CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
16074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 StructuredList, ElementIndex);
16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return hadError && !prevHadError;
160905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
161005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
16114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// Get the structured initializer list for a subobject of type
16124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// @p CurrentObjectType.
16134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListExpr *
16144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
16154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType CurrentObjectType,
16164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
16174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned StructuredIndex,
16184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            SourceRange InitRange) {
16194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *ExistingInit = 0;
16204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
16214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = SyntacticToSemantic[IList];
16224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else if (StructuredIndex < StructuredList->getNumInits())
16234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = StructuredList->getInit(StructuredIndex);
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
16264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return Result;
16274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
16284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (ExistingInit) {
16294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // We are creating an initializer list that initializes the
16304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobjects of the current object, but there was already an
16314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // initialization that completely initialized the current
16324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject, e.g., by a compound literal:
16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
16344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X { int a, b; };
16354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
16374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
16384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // designated initializer re-initializes the whole
16394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject [0], overwriting previous initializers.
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(InitRange.getBegin(),
1641ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                 diag::warn_subobject_initializer_overrides)
16424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << InitRange;
16431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
16444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
164554f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
16464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << ExistingInit->getSourceRange();
16474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
16484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InitListExpr *Result
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
1651ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                                         InitRange.getEnd());
1652ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor
16534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Result->setType(CurrentObjectType);
16544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1655fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  // Pre-allocate storage for the structured initializer list.
1656fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  unsigned NumElements = 0;
165708457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  unsigned NumInits = 0;
165808457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  if (!StructuredList)
165908457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    NumInits = IList->getNumInits();
166008457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  else if (Index < IList->getNumInits()) {
166108457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
166208457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      NumInits = SubList->getNumInits();
166308457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  }
166408457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ArrayType *AType
1666fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1667fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1668fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = CAType->getSize().getZExtValue();
1669fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // Simple heuristic so that we don't allocate a very large
1670fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // initializer with many empty entries at the end.
167108457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      if (NumInits && NumElements > NumInits)
1672fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor        NumElements = 0;
1673fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    }
1674183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
1675fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = VType->getNumElements();
16766217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
1677fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    RecordDecl *RDecl = RType->getDecl();
1678fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (RDecl->isUnion())
1679fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = 1;
1680fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    else
16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NumElements = std::distance(RDecl->field_begin(),
168217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                  RDecl->field_end());
1683fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  }
1684fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
168508457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  if (NumElements < NumInits)
1686fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = IList->getNumInits();
1687fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
1688fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  Result->reserveInits(NumElements);
1689fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
16904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Link this new initializer list into the structured initializer
16914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // lists.
16924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (StructuredList)
16934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredList->updateInit(StructuredIndex, Result);
16944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else {
16954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    Result->setSyntacticForm(IList);
16964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    SyntacticToSemantic[IList] = Result;
16974c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
16984c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
16994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
17004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
17014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
17024c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Update the initializer at index @p StructuredIndex within the
17034c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// structured initializer list to the value @p expr.
17044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
17054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  unsigned &StructuredIndex,
17064c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  Expr *expr) {
17074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // No structured initializer list to update
17084c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
17094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
17104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
17114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
17124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // This initializer overwrites a previous initializer. Warn.
17131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(expr->getSourceRange().getBegin(),
17144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::warn_initializer_overrides)
17154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << expr->getSourceRange();
17161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
17174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
171854f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
17194c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << PrevInit->getSourceRange();
17204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
17211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  ++StructuredIndex;
17234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
17244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
172505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Check that the given Index expression is a valid array designator
172605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// value. This is essentailly just a wrapper around
17273bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner/// VerifyIntegerConstantExpression that also checks for negative values
172805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// and produces a reasonable diagnostic if there is a
172905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// failure. Returns true if there was an error, false otherwise.  If
173005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// everything went okay, Value will receive the value of the constant
173105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// expression.
17321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool
17333bf6893b77c30cb774100e0fa7ae029331675ec1Chris LattnerCheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
173405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation Loc = Index->getSourceRange().getBegin();
173505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
173605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Make sure this is an integer constant expression.
17373bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner  if (S.VerifyIntegerConstantExpression(Index, &Value))
17383bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    return true;
17393bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner
17403bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner  if (Value.isSigned() && Value.isNegative())
17413bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    return S.Diag(Loc, diag::err_array_designator_negative)
174205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      << Value.toString(10) << Index->getSourceRange();
174305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
174453d3d8e0662197f7245d8f5ff697a72a2b4b3f54Douglas Gregor  Value.setIsUnsigned(true);
174505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return false;
174605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
174705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
174805c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
174905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                                        SourceLocation Loc,
1750eeae8f072748affce25ab4064982626361293390Douglas Gregor                                                        bool GNUSyntax,
175105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                                        OwningExprResult Init) {
175205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  typedef DesignatedInitExpr::Designator ASTDesignator;
175305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
175405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool Invalid = false;
175505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  llvm::SmallVector<ASTDesignator, 32> Designators;
175605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  llvm::SmallVector<Expr *, 32> InitExpressions;
175705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
175805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Build designators and check array designator expressions.
175905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
176005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    const Designator &D = Desig.getDesignator(Idx);
176105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    switch (D.getKind()) {
176205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::FieldDesignator:
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
176405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                          D.getFieldLoc()));
176505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
176605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
176705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayDesignator: {
176805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *Index = static_cast<Expr *>(D.getArrayIndex());
176905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt IndexValue;
17709ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      if (!Index->isTypeDependent() &&
17719ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          !Index->isValueDependent() &&
17729ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          CheckArrayDesignatorExpr(*this, Index, IndexValue))
177305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
177405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      else {
177505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Designators.push_back(ASTDesignator(InitExpressions.size(),
17761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            D.getLBracketLoc(),
177705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                            D.getRBracketLoc()));
177805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        InitExpressions.push_back(Index);
177905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
178005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
178105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
178205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
178305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayRangeDesignator: {
178405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
178505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
178605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt StartValue;
178705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt EndValue;
17889ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      bool StartDependent = StartIndex->isTypeDependent() ||
17899ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                            StartIndex->isValueDependent();
17909ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      bool EndDependent = EndIndex->isTypeDependent() ||
17919ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                          EndIndex->isValueDependent();
17929ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      if ((!StartDependent &&
17939ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor           CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
17949ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          (!EndDependent &&
17959ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor           CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
179605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
1797d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor      else {
1798d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        // Make sure we're comparing values with the same bit width.
17999ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        if (StartDependent || EndDependent) {
18009ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          // Nothing to compute.
18019ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
1802d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          EndValue.extend(StartValue.getBitWidth());
1803d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1804d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          StartValue.extend(EndValue.getBitWidth());
1805d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor
1806c4bb7bfe8328f79004517dd268f6146c8066c205Douglas Gregor        if (!StartDependent && !EndDependent && EndValue < StartValue) {
1807d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            << StartValue.toString(10) << EndValue.toString(10)
1809d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor            << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1810d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Invalid = true;
1811d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        } else {
1812d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Designators.push_back(ASTDesignator(InitExpressions.size(),
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              D.getLBracketLoc(),
1814d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getEllipsisLoc(),
1815d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getRBracketLoc()));
1816d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(StartIndex);
1817d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(EndIndex);
1818d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        }
181905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
182005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
182105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
182205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
182305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
182405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
182505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Invalid || Init.isInvalid())
182605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return ExprError();
182705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
182805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Clear out the expressions within the designation.
182905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Desig.ClearExprs(*this);
183005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
183105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  DesignatedInitExpr *DIE
1832beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    = DesignatedInitExpr::Create(Context,
1833beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                 Designators.data(), Designators.size(),
1834beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                 InitExpressions.data(), InitExpressions.size(),
1835e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                 Loc, GNUSyntax, Init.takeAs<Expr>());
183605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return Owned(DIE);
183705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
1838c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
1839cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregorbool Sema::CheckInitList(const InitializedEntity &Entity,
1840cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                         InitListExpr *&InitList, QualType &DeclType) {
1841cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitListChecker CheckInitList(*this, Entity, InitList, DeclType);
1842c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  if (!CheckInitList.HadError())
1843c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor    InitList = CheckInitList.getFullyStructuredList();
1844c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
1845c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  return CheckInitList.HadError();
1846c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor}
184787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
1848cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor//===----------------------------------------------------------------------===//
1849cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor// Initialization entity
1850cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor//===----------------------------------------------------------------------===//
185139da0b8145eaec7da7004f9b3645c5c9f4f63b1dDouglas Gregor
1852cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas GregorInitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
1853cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                     const InitializedEntity &Parent)
1854d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  : Parent(&Parent), Index(Index)
1855cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor{
1856d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
1857d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    Kind = EK_ArrayElement;
1858d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    Type = AT->getElementType();
1859d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  } else {
1860d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    Kind = EK_VectorElement;
1861d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    Type = Parent.getType()->getAs<VectorType>()->getElementType();
1862d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  }
186320093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
186420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
186520093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
186620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                    CXXBaseSpecifier *Base)
186720093b4bf698f292c664676987541d5103b65b15Douglas Gregor{
186820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitializedEntity Result;
186920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Result.Kind = EK_Base;
187020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Result.Base = Base;
1871d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Result.Type = Base->getType();
187220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return Result;
187320093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
187420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
187599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas GregorDeclarationName InitializedEntity::getName() const {
187699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  switch (getKind()) {
187799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Parameter:
1878a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor    if (!VariableOrMember)
1879a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor      return DeclarationName();
1880a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor    // Fall through
1881a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor
1882a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  case EK_Variable:
188399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Member:
188499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return VariableOrMember->getDeclName();
188599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
188699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Result:
188799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Exception:
188818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case EK_New:
188999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Temporary:
189099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Base:
1891d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_ArrayElement:
1892d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_VectorElement:
189399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return DeclarationName();
189499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
189599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
189699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // Silence GCC warning
189799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  return DeclarationName();
189899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor}
189999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
19007abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas GregorDeclaratorDecl *InitializedEntity::getDecl() const {
19017abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  switch (getKind()) {
19027abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Variable:
19037abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Parameter:
19047abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Member:
19057abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    return VariableOrMember;
19067abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
19077abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Result:
19087abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Exception:
19097abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_New:
19107abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Temporary:
19117abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Base:
1912d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_ArrayElement:
1913d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_VectorElement:
19147abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    return 0;
19157abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  }
19167abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
19177abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  // Silence GCC warning
19187abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  return 0;
19197abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor}
19207abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
192120093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
192220093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Initialization sequence
192320093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
192420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
192520093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::Step::Destroy() {
192620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  switch (Kind) {
192720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_ResolveAddressOfOverloadedFunction:
192820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_CastDerivedToBaseRValue:
192920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_CastDerivedToBaseLValue:
193020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_BindReference:
193120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_BindReferenceToTemporary:
193220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_UserConversion:
193320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_QualificationConversionRValue:
193420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_QualificationConversionLValue:
1935d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case SK_ListInitialization:
193651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  case SK_ConstructorInitialization:
193771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  case SK_ZeroInitialization:
193818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CAssignment:
1939cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  case SK_StringInit:
194020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
194120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
194220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_ConversionSequence:
194320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    delete ICS;
194420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
194520093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
194620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
194720093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddAddressOverloadResolutionStep(
194820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                      FunctionDecl *Function) {
194920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
195020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = SK_ResolveAddressOfOverloadedFunction;
195120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = Function->getType();
195220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Function = Function;
195320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
195420093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
195520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
195620093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
195720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                      bool IsLValue) {
195820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
195920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = IsLValue? SK_CastDerivedToBaseLValue : SK_CastDerivedToBaseRValue;
196020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = BaseType;
196120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
196220093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
196320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
196420093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddReferenceBindingStep(QualType T,
196520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                     bool BindingTemporary) {
196620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
196720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
196820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = T;
196920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
197020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
197120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
197203981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedmanvoid InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
197303981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman                                                   QualType T) {
197420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
197520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = SK_UserConversion;
197603981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  S.Type = T;
197720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Function = Function;
197820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
197920093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
198020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
198120093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddQualificationConversionStep(QualType Ty,
198220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                            bool IsLValue) {
198320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
198420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = IsLValue? SK_QualificationConversionLValue
198520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                   : SK_QualificationConversionRValue;
198620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = Ty;
198720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
198820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
198920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
199020093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddConversionSequenceStep(
199120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       const ImplicitConversionSequence &ICS,
199220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       QualType T) {
199320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
199420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = SK_ConversionSequence;
199520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = T;
199620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.ICS = new ImplicitConversionSequence(ICS);
199720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
199820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
199920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2000d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregorvoid InitializationSequence::AddListInitializationStep(QualType T) {
2001d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  Step S;
2002d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  S.Kind = SK_ListInitialization;
2003d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  S.Type = T;
2004d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  Steps.push_back(S);
2005d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor}
2006d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
200751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregorvoid
200851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas GregorInitializationSequence::AddConstructorInitializationStep(
200951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                              CXXConstructorDecl *Constructor,
201051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                                         QualType T) {
201151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  Step S;
201251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  S.Kind = SK_ConstructorInitialization;
201351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  S.Type = T;
201451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  S.Function = Constructor;
201551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  Steps.push_back(S);
201651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor}
201751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
201871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregorvoid InitializationSequence::AddZeroInitializationStep(QualType T) {
201971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  Step S;
202071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  S.Kind = SK_ZeroInitialization;
202171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  S.Type = T;
202271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  Steps.push_back(S);
202371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor}
202471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
202518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregorvoid InitializationSequence::AddCAssignmentStep(QualType T) {
202618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Step S;
202718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  S.Kind = SK_CAssignment;
202818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  S.Type = T;
202918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Steps.push_back(S);
203018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
203118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
2032cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedmanvoid InitializationSequence::AddStringInitStep(QualType T) {
2033cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  Step S;
2034cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  S.Kind = SK_StringInit;
2035cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  S.Type = T;
2036cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  Steps.push_back(S);
2037cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman}
2038cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
203920093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::SetOverloadFailure(FailureKind Failure,
204020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                OverloadingResult Result) {
204120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SequenceKind = FailedSequence;
204220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  this->Failure = Failure;
204320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  this->FailedOverloadResult = Result;
204420093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
204520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
204620093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
204720093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Attempt initialization
204820093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
204920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
205020093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt list initialization (C++0x [dcl.init.list])
2051d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregorstatic void TryListInitialization(Sema &S,
205220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  const InitializedEntity &Entity,
205320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  const InitializationKind &Kind,
205420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  InitListExpr *InitList,
205520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  InitializationSequence &Sequence) {
2056d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // FIXME: We only perform rudimentary checking of list
2057d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // initializations at this point, then assume that any list
2058d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // initialization of an array, aggregate, or scalar will be
2059d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // well-formed. We we actually "perform" list initialization, we'll
2060d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // do all of the necessary checking.  C++0x initializer lists will
2061d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // force us to perform more checking here.
2062d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  Sequence.setSequenceKind(InitializationSequence::ListInitialization);
2063d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
2064d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
2065d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
2066d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // C++ [dcl.init]p13:
2067d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //   If T is a scalar type, then a declaration of the form
2068d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //
2069d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //     T x = { a };
2070d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //
2071d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //   is equivalent to
2072d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //
2073d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  //     T x = a;
2074d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  if (DestType->isScalarType()) {
2075d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) {
2076d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
2077d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      return;
2078d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    }
2079d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
2080d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // Assume scalar initialization from a single value works.
2081d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  } else if (DestType->isAggregateType()) {
2082d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // Assume aggregate initialization works.
2083d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  } else if (DestType->isVectorType()) {
2084d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // Assume vector initialization works.
2085d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  } else if (DestType->isReferenceType()) {
2086d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // FIXME: C++0x defines behavior for this.
2087d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
2088d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    return;
2089d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  } else if (DestType->isRecordType()) {
2090d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // FIXME: C++0x defines behavior for this
2091d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType);
2092d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  }
2093d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
2094d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  // Add a general "list initialization" step.
2095d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  Sequence.AddListInitializationStep(DestType);
209620093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
209720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
209820093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Try a reference initialization that involves calling a conversion
209920093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// function.
210020093b4bf698f292c664676987541d5103b65b15Douglas Gregor///
210120093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// FIXME: look intos DRs 656, 896
210220093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic OverloadingResult TryRefInitWithConversionFunction(Sema &S,
210320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                             const InitializedEntity &Entity,
210420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                             const InitializationKind &Kind,
210520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                          Expr *Initializer,
210620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                          bool AllowRValues,
210720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                             InitializationSequence &Sequence) {
2108d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
210920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
211020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType T1 = cv1T1.getUnqualifiedType();
211120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv2T2 = Initializer->getType();
211220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType T2 = cv2T2.getUnqualifiedType();
211320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
211420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool DerivedToBase;
211520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
211620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         T1, T2, DerivedToBase) &&
211720093b4bf698f292c664676987541d5103b65b15Douglas Gregor         "Must have incompatible references when binding via conversion");
211860cfcecaf48e4310339dcfbdb0e3f0e6d2853855Chandler Carruth  (void)DerivedToBase;
211920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
212020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Build the candidate set directly in the initialization sequence
212120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // structure, so that it will persist if we fail.
212220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
212320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  CandidateSet.clear();
212420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
212520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Determine whether we are allowed to call explicit constructors or
212620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // explicit conversion operators.
212720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
212820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
212920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  const RecordType *T1RecordType = 0;
213020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (AllowRValues && (T1RecordType = T1->getAs<RecordType>())) {
213120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // The type we're converting to is a class type. Enumerate its constructors
213220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // to see if there is a suitable conversion.
213320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
213420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
213520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    DeclarationName ConstructorName
213620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      = S.Context.DeclarationNames.getCXXConstructorName(
213720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                           S.Context.getCanonicalType(T1).getUnqualifiedType());
213820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    DeclContext::lookup_iterator Con, ConEnd;
213920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    for (llvm::tie(Con, ConEnd) = T1RecordDecl->lookup(ConstructorName);
214020093b4bf698f292c664676987541d5103b65b15Douglas Gregor         Con != ConEnd; ++Con) {
214120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Find the constructor (which may be a template).
214220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CXXConstructorDecl *Constructor = 0;
214320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      FunctionTemplateDecl *ConstructorTmpl
214420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(*Con);
214520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConstructorTmpl)
214620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Constructor = cast<CXXConstructorDecl>(
214720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         ConstructorTmpl->getTemplatedDecl());
214820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      else
214920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Constructor = cast<CXXConstructorDecl>(*Con);
215020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
215120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (!Constructor->isInvalidDecl() &&
215220093b4bf698f292c664676987541d5103b65b15Douglas Gregor          Constructor->isConvertingConstructor(AllowExplicit)) {
215320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (ConstructorTmpl)
215420093b4bf698f292c664676987541d5103b65b15Douglas Gregor          S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0,
215520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         &Initializer, 1, CandidateSet);
215620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        else
215720093b4bf698f292c664676987541d5103b65b15Douglas Gregor          S.AddOverloadCandidate(Constructor, &Initializer, 1, CandidateSet);
215820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
215920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
216020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
216120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
216220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (const RecordType *T2RecordType = T2->getAs<RecordType>()) {
216320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // The type we're converting from is a class type, enumerate its conversion
216420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // functions.
216520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
216620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
216720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // Determine the type we are converting to. If we are allowed to
216820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // convert to an rvalue, take the type that the destination type
216920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // refers to.
217020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    QualType ToType = AllowRValues? cv1T1 : DestType;
217120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2172eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall    const UnresolvedSetImpl *Conversions
217320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      = T2RecordDecl->getVisibleConversionFunctions();
2174eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall    for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
2175eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall           E = Conversions->end(); I != E; ++I) {
217620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      NamedDecl *D = *I;
217720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
217820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (isa<UsingShadowDecl>(D))
217920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        D = cast<UsingShadowDecl>(D)->getTargetDecl();
218020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
218120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
218220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CXXConversionDecl *Conv;
218320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConvTemplate)
218420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
218520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      else
218620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Conv = cast<CXXConversionDecl>(*I);
218720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
218820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // If the conversion function doesn't return a reference type,
218920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // it can't be considered for this conversion unless we're allowed to
219020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // consider rvalues.
219120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // FIXME: Do we need to make sure that we only consider conversion
219220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // candidates with reference-compatible results? That might be needed to
219320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // break recursion.
219420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if ((AllowExplicit || !Conv->isExplicit()) &&
219520093b4bf698f292c664676987541d5103b65b15Douglas Gregor          (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
219620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (ConvTemplate)
219720093b4bf698f292c664676987541d5103b65b15Douglas Gregor          S.AddTemplateConversionCandidate(ConvTemplate, ActingDC, Initializer,
219820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           ToType, CandidateSet);
219920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        else
220020093b4bf698f292c664676987541d5103b65b15Douglas Gregor          S.AddConversionCandidate(Conv, ActingDC, Initializer, cv1T1,
220120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                   CandidateSet);
220220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
220320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
220420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
220520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
220620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation DeclLoc = Initializer->getLocStart();
220720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
220820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Perform overload resolution. If it fails, return the failed result.
220920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadCandidateSet::iterator Best;
221020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (OverloadingResult Result
221120093b4bf698f292c664676987541d5103b65b15Douglas Gregor        = S.BestViableFunction(CandidateSet, DeclLoc, Best))
221220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return Result;
221303981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
221420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  FunctionDecl *Function = Best->Function;
221503981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
221603981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // Compute the returned type of the conversion.
221720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (isa<CXXConversionDecl>(Function))
221820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    T2 = Function->getResultType();
221920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  else
222020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    T2 = cv1T1;
222103981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
222203981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // Add the user-defined conversion step.
222303981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  Sequence.AddUserConversionStep(Function, T2.getNonReferenceType());
222403981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
222503981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // Determine whether we need to perform derived-to-base or
222603981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // cv-qualification adjustments.
222720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool NewDerivedToBase = false;
222820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sema::ReferenceCompareResult NewRefRelationship
222920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    = S.CompareReferenceRelationship(DeclLoc, T1, T2.getNonReferenceType(),
223020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     NewDerivedToBase);
223120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  assert(NewRefRelationship != Sema::Ref_Incompatible &&
223220093b4bf698f292c664676987541d5103b65b15Douglas Gregor         "Overload resolution picked a bad conversion function");
223320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  (void)NewRefRelationship;
223420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (NewDerivedToBase)
223520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.AddDerivedToBaseCastStep(
223620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                S.Context.getQualifiedType(T1,
223720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  T2.getNonReferenceType().getQualifiers()),
223820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  /*isLValue=*/true);
223920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
224020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
224120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.AddQualificationConversionStep(cv1T1, T2->isReferenceType());
224220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
224320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
224420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return OR_Success;
224520093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
224620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
224720093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt reference initialization (C++0x [dcl.init.list])
224820093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic void TryReferenceInitialization(Sema &S,
224920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       const InitializedEntity &Entity,
225020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       const InitializationKind &Kind,
225120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       Expr *Initializer,
225220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       InitializationSequence &Sequence) {
225320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sequence.setSequenceKind(InitializationSequence::ReferenceBinding);
225420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2255d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
225620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
22575535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Qualifiers T1Quals;
22585535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
225920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv2T2 = Initializer->getType();
22605535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Qualifiers T2Quals;
22615535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
226220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation DeclLoc = Initializer->getLocStart();
226320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
226420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // If the initializer is the address of an overloaded function, try
226520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // to resolve the overloaded function. If all goes well, T2 is the
226620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // type of the resulting function.
226720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) {
226820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer,
226920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                            T1,
227020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                            false);
227120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (!Fn) {
227220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
227320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return;
227420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
227520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
227620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.AddAddressOverloadResolutionStep(Fn);
227720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    cv2T2 = Fn->getType();
227820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    T2 = cv2T2.getUnqualifiedType();
227920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
228020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
228120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // FIXME: Rvalue references
228220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool ForceRValue = false;
228320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
228420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Compute some basic properties of the types and the initializer.
228520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool isLValueRef = DestType->isLValueReferenceType();
228620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool isRValueRef = !isLValueRef;
228720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool DerivedToBase = false;
228820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression :
228920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                    Initializer->isLvalue(S.Context);
229020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sema::ReferenceCompareResult RefRelationship
229120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase);
229220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
229320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // C++0x [dcl.init.ref]p5:
229420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   A reference to type "cv1 T1" is initialized by an expression of type
229520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   "cv2 T2" as follows:
229620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //
229720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the reference is an lvalue reference and the initializer
229820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       expression
229920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadingResult ConvOvlResult = OR_Success;
230020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (isLValueRef) {
230120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (InitLvalue == Expr::LV_Valid &&
230220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
230320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      //   - is an lvalue (but is not a bit-field), and "cv1 T1" is
230420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      //     reference-compatible with "cv2 T2," or
230520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      //
230620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Per C++ [over.best.ics]p2, we ignore whether the lvalue is a
230720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // bit-field when we're determining whether the reference initialization
230820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // can occur. This property will be checked by PerformInitialization.
230920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (DerivedToBase)
231020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.AddDerivedToBaseCastStep(
23115535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth                         S.Context.getQualifiedType(T1, T2Quals),
231220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                         /*isLValue=*/true);
23135535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth      if (T1Quals != T2Quals)
231420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/true);
231520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/false);
231620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return;
231720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
231820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
231920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //     - has a class type (i.e., T2 is a class type), where T1 is not
232020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       reference-related to T2, and can be implicitly converted to an
232120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
232220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       with "cv3 T3" (this conversion is selected by enumerating the
232320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       applicable conversion functions (13.3.1.6) and choosing the best
232420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       one through overload resolution (13.3)),
232520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType()) {
232620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
232720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Initializer,
232820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       /*AllowRValues=*/false,
232920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Sequence);
233020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConvOvlResult == OR_Success)
233120093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return;
23321d31833450e6d2947a33cb0840d87661d92eec07John McCall      if (ConvOvlResult != OR_No_Viable_Function) {
23331d31833450e6d2947a33cb0840d87661d92eec07John McCall        Sequence.SetOverloadFailure(
23341d31833450e6d2947a33cb0840d87661d92eec07John McCall                      InitializationSequence::FK_ReferenceInitOverloadFailed,
23351d31833450e6d2947a33cb0840d87661d92eec07John McCall                                    ConvOvlResult);
23361d31833450e6d2947a33cb0840d87661d92eec07John McCall      }
233720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
233820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
233920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
234020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - Otherwise, the reference shall be an lvalue reference to a
234120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       non-volatile const type (i.e., cv1 shall be const), or the reference
234220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       shall be an rvalue reference and the initializer expression shall
234320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       be an rvalue.
23445535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  if (!((isLValueRef && T1Quals.hasConst()) ||
234520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        (isRValueRef && InitLvalue != Expr::LV_Valid))) {
234620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
234720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.SetOverloadFailure(
234820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                        InitializationSequence::FK_ReferenceInitOverloadFailed,
234920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  ConvOvlResult);
235020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else if (isLValueRef)
235120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.SetFailed(InitLvalue == Expr::LV_Valid
235220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        ? (RefRelationship == Sema::Ref_Related
235320093b4bf698f292c664676987541d5103b65b15Douglas Gregor             ? InitializationSequence::FK_ReferenceInitDropsQualifiers
235420093b4bf698f292c664676987541d5103b65b15Douglas Gregor             : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
235520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
235620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
235720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.SetFailed(
235820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                    InitializationSequence::FK_RValueReferenceBindingToLValue);
235920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
236020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
236120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
236220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
236320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       - If T1 and T2 are class types and
236420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (T1->isRecordType() && T2->isRecordType()) {
236520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       - the initializer expression is an rvalue and "cv1 T1" is
236620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //         reference-compatible with "cv2 T2", or
236720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (InitLvalue != Expr::LV_Valid &&
236820093b4bf698f292c664676987541d5103b65b15Douglas Gregor        RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
236920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (DerivedToBase)
237020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.AddDerivedToBaseCastStep(
23715535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth                         S.Context.getQualifiedType(T1, T2Quals),
237220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                         /*isLValue=*/false);
23735535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth      if (T1Quals != T2Quals)
237420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/false);
237520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
237620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return;
237720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
237820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
237920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       - T1 is not reference-related to T2 and the initializer expression
238020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //         can be implicitly converted to an rvalue of type "cv3 T3" (this
238120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //         conversion is selected by enumerating the applicable conversion
238220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //         functions (13.3.1.6) and choosing the best one through overload
238320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //         resolution (13.3)),
238420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (RefRelationship == Sema::Ref_Incompatible) {
238520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
238620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Kind, Initializer,
238720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       /*AllowRValues=*/true,
238820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Sequence);
238920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConvOvlResult)
239020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.SetOverloadFailure(
239120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                      InitializationSequence::FK_ReferenceInitOverloadFailed,
239220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                    ConvOvlResult);
239320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
239420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return;
239520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
239620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
239720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
239820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
239920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
240020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
240120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      - If the initializer expression is an rvalue, with T2 an array type,
240220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        and "cv1 T1" is reference-compatible with "cv2 T2," the reference
240320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        is bound to the object represented by the rvalue (see 3.10).
240420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // FIXME: How can an array type be reference-compatible with anything?
240520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Don't we mean the element types of T1 and T2?
240620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
240720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      - Otherwise, a temporary of type “cv1 T1” is created and initialized
240820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        from the initializer expression using the rules for a non-reference
240920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        copy initialization (8.5). The reference is then bound to the
241020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        temporary. [...]
241120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Determine whether we are allowed to call explicit constructors or
241220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // explicit conversion operators.
241320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct);
241420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ImplicitConversionSequence ICS
241520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    = S.TryImplicitConversion(Initializer, cv1T1,
241620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*SuppressUserConversions=*/false, AllowExplicit,
241720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*ForceRValue=*/false,
241820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*FIXME:InOverloadResolution=*/false,
241920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*UserCast=*/Kind.isExplicitCast());
242020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
24211d31833450e6d2947a33cb0840d87661d92eec07John McCall  if (ICS.isBad()) {
242220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // FIXME: Use the conversion function set stored in ICS to turn
242320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // this into an overloading ambiguity diagnostic. However, we need
242420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // to keep that set as an OverloadCandidateSet rather than as some
242520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // other kind of set.
242618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
242718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      Sequence.SetOverloadFailure(
242818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                        InitializationSequence::FK_ReferenceInitOverloadFailed,
242918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                  ConvOvlResult);
243018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    else
243118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
243220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
243320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
243420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
243520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        [...] If T1 is reference-related to T2, cv1 must be the
243620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        same cv-qualification as, or greater cv-qualification
243720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        than, cv2; otherwise, the program is ill-formed.
24385535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
24395535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
244020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (RefRelationship == Sema::Ref_Related &&
24415535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth      (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
244220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
244320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
244420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
244520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
244620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Perform the actual conversion.
244720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sequence.AddConversionSequenceStep(ICS, cv1T1);
244820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
244920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return;
245020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
245120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
245220093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt character array initialization from a string literal
245320093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// (C++ [dcl.init.string], C99 6.7.8).
245420093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic void TryStringLiteralInitialization(Sema &S,
245520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           const InitializedEntity &Entity,
245620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           const InitializationKind &Kind,
245720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           Expr *Initializer,
245820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       InitializationSequence &Sequence) {
2459cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  Sequence.setSequenceKind(InitializationSequence::StringInit);
2460d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Sequence.AddStringInitStep(Entity.getType());
246120093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
246220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
246320093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt initialization by constructor (C++ [dcl.init]), which
246420093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// enumerates the constructors of the initialized entity and performs overload
246520093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// resolution to select the best.
246620093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic void TryConstructorInitialization(Sema &S,
246720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         const InitializedEntity &Entity,
246820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         const InitializationKind &Kind,
246920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         Expr **Args, unsigned NumArgs,
247071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                         QualType DestType,
247120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         InitializationSequence &Sequence) {
247218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (Kind.getKind() == InitializationKind::IK_Copy)
247318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
247418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  else
247518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization);
247651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
247751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // Build the candidate set directly in the initialization sequence
247851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // structure, so that it will persist if we fail.
247951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
248051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  CandidateSet.clear();
248151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
248251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // Determine whether we are allowed to call explicit constructors or
248351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // explicit conversion operators.
248451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct ||
248551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                        Kind.getKind() == InitializationKind::IK_Value ||
248651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                        Kind.getKind() == InitializationKind::IK_Default);
248751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
248851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // The type we're converting to is a class type. Enumerate its constructors
248951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // to see if one is suitable.
249051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  const RecordType *DestRecordType = DestType->getAs<RecordType>();
249151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  assert(DestRecordType && "Constructor initialization requires record type");
249251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  CXXRecordDecl *DestRecordDecl
249351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    = cast<CXXRecordDecl>(DestRecordType->getDecl());
249451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
249551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  DeclarationName ConstructorName
249651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    = S.Context.DeclarationNames.getCXXConstructorName(
249751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                     S.Context.getCanonicalType(DestType).getUnqualifiedType());
249851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  DeclContext::lookup_iterator Con, ConEnd;
249951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName);
250051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor       Con != ConEnd; ++Con) {
250151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    // Find the constructor (which may be a template).
250251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    CXXConstructorDecl *Constructor = 0;
250351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    FunctionTemplateDecl *ConstructorTmpl
250451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      = dyn_cast<FunctionTemplateDecl>(*Con);
250551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    if (ConstructorTmpl)
250651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      Constructor = cast<CXXConstructorDecl>(
250751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                           ConstructorTmpl->getTemplatedDecl());
250851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    else
250951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      Constructor = cast<CXXConstructorDecl>(*Con);
251051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
251151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    if (!Constructor->isInvalidDecl() &&
251299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor        (AllowExplicit || !Constructor->isExplicit())) {
251351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      if (ConstructorTmpl)
251451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0,
251551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                       Args, NumArgs, CandidateSet);
251651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      else
251751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet);
251851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    }
251951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  }
252051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
252151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  SourceLocation DeclLoc = Kind.getLocation();
252251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
252351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // Perform overload resolution. If it fails, return the failed result.
252451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  OverloadCandidateSet::iterator Best;
252551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  if (OverloadingResult Result
252651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        = S.BestViableFunction(CandidateSet, DeclLoc, Best)) {
252751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    Sequence.SetOverloadFailure(
252851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                          InitializationSequence::FK_ConstructorOverloadFailed,
252951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                Result);
253051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    return;
253151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  }
253251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
253351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // Add the constructor initialization step. Any cv-qualification conversion is
253451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  // subsumed by the initialization.
253518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (Kind.getKind() == InitializationKind::IK_Copy) {
253618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Sequence.AddUserConversionStep(Best->Function, DestType);
253718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  } else {
253818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Sequence.AddConstructorInitializationStep(
253951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                      cast<CXXConstructorDecl>(Best->Function),
254018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                      DestType);
254118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
254220093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
254320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
254471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor/// \brief Attempt value initialization (C++ [dcl.init]p7).
254571d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregorstatic void TryValueInitialization(Sema &S,
254671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                   const InitializedEntity &Entity,
254771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                   const InitializationKind &Kind,
254871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                   InitializationSequence &Sequence) {
254971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  // C++ [dcl.init]p5:
255071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  //
255171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  //   To value-initialize an object of type T means:
2552d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType T = Entity.getType();
255371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
255471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  //     -- if T is an array type, then each element is value-initialized;
255571d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  while (const ArrayType *AT = S.Context.getAsArrayType(T))
255671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    T = AT->getElementType();
255771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
255871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  if (const RecordType *RT = T->getAs<RecordType>()) {
255971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
256071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      // -- if T is a class type (clause 9) with a user-declared
256171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      //    constructor (12.1), then the default constructor for T is
256271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      //    called (and the initialization is ill-formed if T has no
256371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      //    accessible default constructor);
256471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      //
256571d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      // FIXME: we really want to refer to a single subobject of the array,
256671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      // but Entity doesn't have a way to capture that (yet).
256771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      if (ClassDecl->hasUserDeclaredConstructor())
256871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor        return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
256971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
257016006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      // -- if T is a (possibly cv-qualified) non-union class type
257116006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      //    without a user-provided constructor, then the object is
257216006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      //    zero-initialized and, if T’s implicitly-declared default
257316006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      //    constructor is non-trivial, that constructor is called.
257416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      if ((ClassDecl->getTagKind() == TagDecl::TK_class ||
257516006c901315fa12a108b4e571f187f4b676e426Douglas Gregor           ClassDecl->getTagKind() == TagDecl::TK_struct) &&
257616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor          !ClassDecl->hasTrivialConstructor()) {
2577d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor        Sequence.AddZeroInitializationStep(Entity.getType());
257816006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
257916006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      }
258071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    }
258171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  }
258271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
2583d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Sequence.AddZeroInitializationStep(Entity.getType());
258471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  Sequence.setSequenceKind(InitializationSequence::ZeroInitialization);
258571d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor}
258671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
258799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor/// \brief Attempt default initialization (C++ [dcl.init]p6).
258899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregorstatic void TryDefaultInitialization(Sema &S,
258999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                     const InitializedEntity &Entity,
259099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                     const InitializationKind &Kind,
259199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                     InitializationSequence &Sequence) {
259299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  assert(Kind.getKind() == InitializationKind::IK_Default);
259399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
259499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // C++ [dcl.init]p6:
259599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   To default-initialize an object of type T means:
259699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //     - if T is an array type, each element is default-initialized;
2597d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
259899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  while (const ArrayType *Array = S.Context.getAsArrayType(DestType))
259999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    DestType = Array->getElementType();
260099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
260199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //     - if T is a (possibly cv-qualified) class type (Clause 9), the default
260299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //       constructor for T is called (and the initialization is ill-formed if
260399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //       T has no accessible default constructor);
260499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (DestType->isRecordType()) {
260599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    // FIXME: If a program calls for the default initialization of an object of
260699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    // a const-qualified type T, T shall be a class type with a user-provided
260799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    // default constructor.
260899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType,
260999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                        Sequence);
261099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
261199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
261299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //     - otherwise, no initialization is performed.
261399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  Sequence.setSequenceKind(InitializationSequence::NoInitialization);
261499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
261599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   If a program calls for the default initialization of an object of
261699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   a const-qualified type T, T shall be a class type with a user-provided
261799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   default constructor.
261899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (DestType.isConstQualified())
261999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
262099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor}
262199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
262220093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
262320093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// which enumerates all conversion functions and performs overload resolution
262420093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// to select the best.
262520093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic void TryUserDefinedConversion(Sema &S,
262620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     const InitializedEntity &Entity,
262720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     const InitializationKind &Kind,
262820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     Expr *Initializer,
262920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     InitializationSequence &Sequence) {
26304a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
26314a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
2632d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
26334a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  assert(!DestType->isReferenceType() && "References are handled elsewhere");
26344a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  QualType SourceType = Initializer->getType();
26354a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  assert((DestType->isRecordType() || SourceType->isRecordType()) &&
26364a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor         "Must have a class type to perform a user-defined conversion");
26374a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
26384a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Build the candidate set directly in the initialization sequence
26394a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // structure, so that it will persist if we fail.
26404a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
26414a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  CandidateSet.clear();
26424a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
26434a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Determine whether we are allowed to call explicit constructors or
26444a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // explicit conversion operators.
26454a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
26464a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
26474a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
26484a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // The type we're converting to is a class type. Enumerate its constructors
26494a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // to see if there is a suitable conversion.
26504a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    CXXRecordDecl *DestRecordDecl
26514a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      = cast<CXXRecordDecl>(DestRecordType->getDecl());
26524a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
26534a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    DeclarationName ConstructorName
26544a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      = S.Context.DeclarationNames.getCXXConstructorName(
26554a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor                     S.Context.getCanonicalType(DestType).getUnqualifiedType());
26564a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    DeclContext::lookup_iterator Con, ConEnd;
26574a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName);
26584a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor         Con != ConEnd; ++Con) {
26594a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      // Find the constructor (which may be a template).
26604a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      CXXConstructorDecl *Constructor = 0;
26614a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      FunctionTemplateDecl *ConstructorTmpl
26624a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        = dyn_cast<FunctionTemplateDecl>(*Con);
26634a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      if (ConstructorTmpl)
26644a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        Constructor = cast<CXXConstructorDecl>(
26654a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor                                           ConstructorTmpl->getTemplatedDecl());
26664a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      else
26674a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        Constructor = cast<CXXConstructorDecl>(*Con);
26684a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
26694a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      if (!Constructor->isInvalidDecl() &&
26704a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor          Constructor->isConvertingConstructor(AllowExplicit)) {
26714a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        if (ConstructorTmpl)
26724a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor          S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0,
26734a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor                                         &Initializer, 1, CandidateSet);
26744a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        else
26754a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor          S.AddOverloadCandidate(Constructor, &Initializer, 1, CandidateSet);
26764a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      }
26774a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    }
26784a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
2679cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
2680cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  SourceLocation DeclLoc = Initializer->getLocStart();
2681cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
26824a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
26834a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // The type we're converting from is a class type, enumerate its conversion
26844a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // functions.
2685cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
268633c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman    // We can only enumerate the conversion functions for a complete type; if
268733c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman    // the type isn't complete, simply skip this step.
268833c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman    if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
268933c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman      CXXRecordDecl *SourceRecordDecl
269033c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        = cast<CXXRecordDecl>(SourceRecordType->getDecl());
26914a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
2692eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall      const UnresolvedSetImpl *Conversions
269333c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        = SourceRecordDecl->getVisibleConversionFunctions();
2694eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall      for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
269533c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman           E = Conversions->end();
269633c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman           I != E; ++I) {
269733c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        NamedDecl *D = *I;
269833c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
269933c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        if (isa<UsingShadowDecl>(D))
270033c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          D = cast<UsingShadowDecl>(D)->getTargetDecl();
270133c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman
270233c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
270333c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        CXXConversionDecl *Conv;
27044a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        if (ConvTemplate)
270533c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
27064a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        else
270733c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          Conv = cast<CXXConversionDecl>(*I);
270833c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman
270933c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        if (AllowExplicit || !Conv->isExplicit()) {
271033c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          if (ConvTemplate)
271133c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman            S.AddTemplateConversionCandidate(ConvTemplate, ActingDC,
271233c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman                                             Initializer, DestType,
271333c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman                                             CandidateSet);
271433c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          else
271533c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman            S.AddConversionCandidate(Conv, ActingDC, Initializer, DestType,
271633c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman                                     CandidateSet);
271733c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        }
27184a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      }
27194a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    }
27204a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
27214a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
27224a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Perform overload resolution. If it fails, return the failed result.
27234a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  OverloadCandidateSet::iterator Best;
27241d31833450e6d2947a33cb0840d87661d92eec07John McCall  if (OverloadingResult Result
27254a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        = S.BestViableFunction(CandidateSet, DeclLoc, Best)) {
27264a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    Sequence.SetOverloadFailure(
27274a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor                        InitializationSequence::FK_UserConversionOverloadFailed,
27284a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor                                Result);
27294a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    return;
27304a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
27311d31833450e6d2947a33cb0840d87661d92eec07John McCall
27324a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  FunctionDecl *Function = Best->Function;
27334a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
27344a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (isa<CXXConstructorDecl>(Function)) {
27354a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // Add the user-defined conversion step. Any cv-qualification conversion is
27364a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // subsumed by the initialization.
27374a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    Sequence.AddUserConversionStep(Function, DestType);
27384a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    return;
27394a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
27404a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
27414a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Add the user-defined conversion step that calls the conversion function.
27424a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  QualType ConvType = Function->getResultType().getNonReferenceType();
27434a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  Sequence.AddUserConversionStep(Function, ConvType);
27444a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
27454a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // If the conversion following the call to the conversion function is
27464a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // interesting, add it as a separate step.
27474a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (Best->FinalConversion.First || Best->FinalConversion.Second ||
27484a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      Best->FinalConversion.Third) {
27494a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    ImplicitConversionSequence ICS;
27501d31833450e6d2947a33cb0840d87661d92eec07John McCall    ICS.setStandard();
27514a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    ICS.Standard = Best->FinalConversion;
27524a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    Sequence.AddConversionSequenceStep(ICS, DestType);
27534a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
275420093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
275520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
275620093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt an implicit conversion (C++ [conv]) converting from one
275720093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// non-class type to another.
275820093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic void TryImplicitConversion(Sema &S,
275920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  const InitializedEntity &Entity,
276020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  const InitializationKind &Kind,
276120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  Expr *Initializer,
276220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  InitializationSequence &Sequence) {
276320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ImplicitConversionSequence ICS
2764d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    = S.TryImplicitConversion(Initializer, Entity.getType(),
276520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*SuppressUserConversions=*/true,
276620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*AllowExplicit=*/false,
276720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*ForceRValue=*/false,
276820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*FIXME:InOverloadResolution=*/false,
276920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              /*UserCast=*/Kind.isExplicitCast());
277020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
27711d31833450e6d2947a33cb0840d87661d92eec07John McCall  if (ICS.isBad()) {
277220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_ConversionFailed);
277320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
277420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
277520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2776d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Sequence.AddConversionSequenceStep(ICS, Entity.getType());
277720093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
277820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
277920093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializationSequence::InitializationSequence(Sema &S,
278020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               const InitializedEntity &Entity,
278120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               const InitializationKind &Kind,
278220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               Expr **Args,
278320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               unsigned NumArgs) {
278420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ASTContext &Context = S.Context;
278520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
278620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // C++0x [dcl.init]p16:
278720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   The semantics of initializers are as follows. The destination type is
278820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   the type of the object or reference being initialized and the source
278920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   type is the type of the initializer expression. The source type is not
279020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   defined when the initializer is a braced-init-list or when it is a
279120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   parenthesized list of expressions.
2792d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
279320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
279420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (DestType->isDependentType() ||
279520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
279620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SequenceKind = DependentSequence;
279720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
279820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
279920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
280020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType SourceType;
280120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Expr *Initializer = 0;
280299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (NumArgs == 1) {
280320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Initializer = Args[0];
280420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (!isa<InitListExpr>(Initializer))
280520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      SourceType = Initializer->getType();
280620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
280720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
280820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the initializer is a braced-init-list, the object is
280920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       list-initialized (8.5.4).
281020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
281120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    TryListInitialization(S, Entity, Kind, InitList, *this);
2812d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    return;
281320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
281420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
281520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the destination type is a reference type, see 8.5.3.
281620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (DestType->isReferenceType()) {
281720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // C++0x [dcl.init.ref]p1:
281820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //   A variable declared to be a T& or T&&, that is, "reference to type T"
281920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //   (8.3.2), shall be initialized by an object, or function, of type T or
282020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //   by an object that can be converted into a T.
282120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // (Therefore, multiple arguments are not permitted.)
282220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (NumArgs != 1)
282320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      SetFailed(FK_TooManyInitsForReference);
282420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
282520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
282620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
282720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
282820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
282920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the destination type is an array of characters, an array of
283020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       char16_t, an array of char32_t, or an array of wchar_t, and the
283120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       initializer is a string literal, see 8.5.2.
283220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (Initializer && IsStringInit(Initializer, DestType, Context)) {
283320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
283420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
283520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
283620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
283720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the initializer is (), the object is value-initialized.
283899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (Kind.getKind() == InitializationKind::IK_Value ||
283999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
284020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    TryValueInitialization(S, Entity, Kind, *this);
284120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
284220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
284320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
284499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // Handle default initialization.
284599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (Kind.getKind() == InitializationKind::IK_Default){
284699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    TryDefaultInitialization(S, Entity, Kind, *this);
284799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return;
284899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
284918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
285020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - Otherwise, if the destination type is an array, the program is
285120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       ill-formed.
285220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (const ArrayType *AT = Context.getAsArrayType(DestType)) {
285320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (AT->getElementType()->isAnyCharacterType())
285420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
285520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
285620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      SetFailed(FK_ArrayNeedsInitList);
285720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
285820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
285920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
2860cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
2861cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  // Handle initialization in C
2862cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  if (!S.getLangOptions().CPlusPlus) {
2863cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    setSequenceKind(CAssignment);
2864cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    AddCAssignmentStep(DestType);
2865cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    return;
2866cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  }
286720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
286820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the destination type is a (possibly cv-qualified) class type:
286920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (DestType->isRecordType()) {
287020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //     - If the initialization is direct-initialization, or if it is
287120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       copy-initialization where the cv-unqualified version of the
287220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       source type is the same class as, or a derived class of, the
287320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       class of the destination, constructors are considered. [...]
287420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (Kind.getKind() == InitializationKind::IK_Direct ||
287520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        (Kind.getKind() == InitializationKind::IK_Copy &&
287620093b4bf698f292c664676987541d5103b65b15Douglas Gregor         (Context.hasSameUnqualifiedType(SourceType, DestType) ||
287720093b4bf698f292c664676987541d5103b65b15Douglas Gregor          S.IsDerivedFrom(SourceType, DestType))))
287871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
2879d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor                                   Entity.getType(), *this);
288020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //     - Otherwise (i.e., for the remaining copy-initialization cases),
288120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       user-defined conversion sequences that can convert from the source
288220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       type to the destination type or (when a conversion function is
288320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       used) to a derived class thereof are enumerated as described in
288420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       13.3.1.4, and the best one is chosen through overload resolution
288520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       (13.3).
288620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
288720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
288820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
288920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
289020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
289199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (NumArgs > 1) {
289299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    SetFailed(FK_TooManyInitsForScalar);
289399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return;
289499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
289599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  assert(NumArgs == 1 && "Zero-argument case handled above");
289699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
289720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //    - Otherwise, if the source type is a (possibly cv-qualified) class
289820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      type, conversion functions are considered.
289999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (!SourceType.isNull() && SourceType->isRecordType()) {
290020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
290120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
290220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
290320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
290420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //    - Otherwise, the initial value of the object being initialized is the
29054a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  //      (possibly converted) value of the initializer expression. Standard
290620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      conversions (Clause 4) will be used, if necessary, to convert the
290720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      initializer expression to the cv-unqualified version of the
290820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      destination type; no user-defined conversions are considered.
290999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  setSequenceKind(StandardConversion);
291020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  TryImplicitConversion(S, Entity, Kind, Initializer, *this);
291120093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
291220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
291320093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializationSequence::~InitializationSequence() {
291420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(),
291520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                          StepEnd = Steps.end();
291620093b4bf698f292c664676987541d5103b65b15Douglas Gregor       Step != StepEnd; ++Step)
291720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Step->Destroy();
291820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
291920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
292020093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
292120093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Perform initialization
292220093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
292318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregorstatic Sema::AssignmentAction
292418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas GregorgetAssignmentAction(const InitializedEntity &Entity) {
292518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  switch(Entity.getKind()) {
292618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Variable:
292718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_New:
292818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Initializing;
292918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
293018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Parameter:
293118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    // FIXME: Can we tell when we're sending vs. passing?
293218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Passing;
293318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
293418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Result:
293518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Returning;
293618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
293718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Exception:
293818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Base:
293918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    llvm_unreachable("No assignment action for C++-specific initialization");
294018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
294118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
294218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Temporary:
294318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    // FIXME: Can we tell apart casting vs. converting?
294418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Casting;
294518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
294618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Member:
2947d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_ArrayElement:
2948d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_VectorElement:
294918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Initializing;
295018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
295118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
295218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  return Sema::AA_Converting;
295318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
295418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
295518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregorstatic bool shouldBindAsTemporary(const InitializedEntity &Entity,
295618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                  bool IsCopy) {
295718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  switch (Entity.getKind()) {
295818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Result:
295918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Exception:
296018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return !IsCopy;
296118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
296218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_New:
296318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Variable:
296418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Base:
296518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Member:
2966d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_ArrayElement:
2967d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_VectorElement:
296818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return false;
296918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
297018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Parameter:
297118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Temporary:
297218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return true;
297318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
297418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
297518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  llvm_unreachable("missed an InitializedEntity kind?");
297618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
297718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
297818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor/// \brief If we need to perform an additional copy of the initialized object
297918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor/// for this kind of entity (e.g., the result of a function or an object being
298018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor/// thrown), make the copy.
298118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregorstatic Sema::OwningExprResult CopyIfRequiredForEntity(Sema &S,
298218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                            const InitializedEntity &Entity,
29837abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor                                             const InitializationKind &Kind,
298418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                             Sema::OwningExprResult CurInit) {
298518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  SourceLocation Loc;
298618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
298718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  switch (Entity.getKind()) {
298818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Result:
2989d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    if (Entity.getType()->isReferenceType())
299018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      return move(CurInit);
299118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Loc = Entity.getReturnLoc();
299218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
299318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
299418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Exception:
299518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Loc = Entity.getThrowLoc();
299618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
299718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
299818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Variable:
2999d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    if (Entity.getType()->isReferenceType() ||
30007abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor        Kind.getKind() != InitializationKind::IK_Copy)
30017abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      return move(CurInit);
30027abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    Loc = Entity.getDecl()->getLocation();
30037abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    break;
30047abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
300518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Parameter:
30067abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    // FIXME: Do we need this initialization for a parameter?
30077abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    return move(CurInit);
30087abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
300918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_New:
301018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Temporary:
301118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Base:
301218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Member:
3013d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_ArrayElement:
3014d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_VectorElement:
301518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    // We don't need to copy for any of these initialized entities.
301618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return move(CurInit);
301718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
301818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
301918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Expr *CurInitExpr = (Expr *)CurInit.get();
302018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  CXXRecordDecl *Class = 0;
302118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>())
302218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    Class = cast<CXXRecordDecl>(Record->getDecl());
302318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (!Class)
302418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return move(CurInit);
302518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
302618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  // Perform overload resolution using the class's copy constructors.
302718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  DeclarationName ConstructorName
302818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    = S.Context.DeclarationNames.getCXXConstructorName(
302918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                  S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)));
303018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  DeclContext::lookup_iterator Con, ConEnd;
303118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  OverloadCandidateSet CandidateSet;
303218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  for (llvm::tie(Con, ConEnd) = Class->lookup(ConstructorName);
303318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor       Con != ConEnd; ++Con) {
303418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    // Find the constructor (which may be a template).
303518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(*Con);
303618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    if (!Constructor || Constructor->isInvalidDecl() ||
30379e9199d8649cf3e10c98a69403f05dbb666d8fb1Douglas Gregor        !Constructor->isCopyConstructor())
303818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      continue;
303918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
304018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.AddOverloadCandidate(Constructor, &CurInitExpr, 1, CandidateSet);
304118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
304218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
304318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  OverloadCandidateSet::iterator Best;
304418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  switch (S.BestViableFunction(CandidateSet, Loc, Best)) {
304518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_Success:
304618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
304718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
304818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_No_Viable_Function:
304918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Loc, diag::err_temp_copy_no_viable)
30507abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      << (int)Entity.getKind() << CurInitExpr->getType()
305118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << CurInitExpr->getSourceRange();
3052cbce60633c9864261105b289473e5a3ed7b4a729John McCall    S.PrintOverloadCandidates(CandidateSet, Sema::OCD_AllCandidates,
3053cbce60633c9864261105b289473e5a3ed7b4a729John McCall                              &CurInitExpr, 1);
305418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return S.ExprError();
305518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
305618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_Ambiguous:
305718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Loc, diag::err_temp_copy_ambiguous)
30587abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      << (int)Entity.getKind() << CurInitExpr->getType()
305918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << CurInitExpr->getSourceRange();
3060cbce60633c9864261105b289473e5a3ed7b4a729John McCall    S.PrintOverloadCandidates(CandidateSet, Sema::OCD_ViableCandidates,
3061cbce60633c9864261105b289473e5a3ed7b4a729John McCall                              &CurInitExpr, 1);
306218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return S.ExprError();
306318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
306418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_Deleted:
306518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Loc, diag::err_temp_copy_deleted)
30667abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      << (int)Entity.getKind() << CurInitExpr->getType()
306718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << CurInitExpr->getSourceRange();
306818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
306918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << Best->Function->isDeleted();
307018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return S.ExprError();
307118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
307218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
307318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  CurInit.release();
307418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  return S.BuildCXXConstructExpr(Loc, CurInitExpr->getType(),
307518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                 cast<CXXConstructorDecl>(Best->Function),
307618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                 /*Elidable=*/true,
307718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                 Sema::MultiExprArg(S,
307818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                                    (void**)&CurInitExpr, 1));
307918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
308020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
308120093b4bf698f292c664676987541d5103b65b15Douglas GregorAction::OwningExprResult
308220093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializationSequence::Perform(Sema &S,
308320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                const InitializedEntity &Entity,
308420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                const InitializationKind &Kind,
3085d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                Action::MultiExprArg Args,
3086d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                QualType *ResultType) {
308720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (SequenceKind == FailedSequence) {
308820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    unsigned NumArgs = Args.size();
308920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs);
309020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return S.ExprError();
309120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
309220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
309320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (SequenceKind == DependentSequence) {
3094d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // If the declaration is a non-dependent, incomplete array type
3095d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // that has an initializer, then its type will be completed once
3096d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // the initializer is instantiated.
3097d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    if (ResultType && !Entity.getType()->isDependentType() &&
3098d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        Args.size() == 1) {
3099d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor      QualType DeclType = Entity.getType();
3100d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      if (const IncompleteArrayType *ArrayT
3101d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                           = S.Context.getAsIncompleteArrayType(DeclType)) {
3102d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // FIXME: We don't currently have the ability to accurately
3103d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // compute the length of an initializer list without
3104d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // performing full type-checking of the initializer list
3105d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // (since we have to determine where braces are implicitly
3106d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // introduced and such).  So, we fall back to making the array
3107d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // type a dependently-sized array type with no specified
3108d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // bound.
3109d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        if (isa<InitListExpr>((Expr *)Args.get()[0])) {
3110d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          SourceRange Brackets;
3111d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor
3112d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          // Scavange the location of the brackets from the entity, if we can.
3113d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor          if (DeclaratorDecl *DD = Entity.getDecl()) {
3114d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor            if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) {
3115d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor              TypeLoc TL = TInfo->getTypeLoc();
3116d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor              if (IncompleteArrayTypeLoc *ArrayLoc
3117d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor                                      = dyn_cast<IncompleteArrayTypeLoc>(&TL))
3118d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor              Brackets = ArrayLoc->getBracketsRange();
3119d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor            }
3120d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          }
3121d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3122d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          *ResultType
3123d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor            = S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
3124d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                                   /*NumElts=*/0,
3125d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                                   ArrayT->getSizeModifier(),
3126d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                       ArrayT->getIndexTypeCVRQualifiers(),
3127d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                                   Brackets);
3128d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        }
3129d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3130d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      }
3131d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    }
3132d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3133085446216a198ced4183ec1571e1ae51c2920e98Eli Friedman    if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast())
313420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return Sema::OwningExprResult(S, Args.release()[0]);
313520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
313620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    unsigned NumArgs = Args.size();
313720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return S.Owned(new (S.Context) ParenListExpr(S.Context,
313820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                 SourceLocation(),
313920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                 (Expr **)Args.release(),
314020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                 NumArgs,
314120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                 SourceLocation()));
314220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
314320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
314499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (SequenceKind == NoInitialization)
314599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return S.Owned((Expr *)0);
314699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
3147d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType().getNonReferenceType();
3148d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  // FIXME: Ugly hack around the fact that Entity.getType() is not
3149a91eb541a6adf81acf872e7315bc6b814c3241ebEli Friedman  // the same as Entity.getDecl()->getType() in cases involving type merging,
3150a91eb541a6adf81acf872e7315bc6b814c3241ebEli Friedman  //  and we want latter when it makes sense.
3151d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  if (ResultType)
3152a91eb541a6adf81acf872e7315bc6b814c3241ebEli Friedman    *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() :
3153d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor                                     Entity.getType();
315420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
315599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  Sema::OwningExprResult CurInit = S.Owned((Expr *)0);
315699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
315799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  assert(!Steps.empty() && "Cannot have an empty initialization sequence");
315899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
315999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // For initialization steps that start with a single initializer,
316099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // grab the only argument out the Args and place it into the "current"
316199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // initializer.
316299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  switch (Steps.front().Kind) {
316318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ResolveAddressOfOverloadedFunction:
316418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CastDerivedToBaseRValue:
316518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CastDerivedToBaseLValue:
316618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_BindReference:
316718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_BindReferenceToTemporary:
316818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_UserConversion:
316918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_QualificationConversionLValue:
317018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_QualificationConversionRValue:
317118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ConversionSequence:
317218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ListInitialization:
317318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CAssignment:
3174cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  case SK_StringInit:
317518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    assert(Args.size() == 1);
317618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    CurInit = Sema::OwningExprResult(S, ((Expr **)(Args.get()))[0]->Retain());
317718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    if (CurInit.isInvalid())
317818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      return S.ExprError();
317918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
318018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
318118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ConstructorInitialization:
318218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ZeroInitialization:
318318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
318420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
318520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
318620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Walk through the computed steps for the initialization sequence,
318720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // performing the specified conversions along the way.
318816006c901315fa12a108b4e571f187f4b676e426Douglas Gregor  bool ConstructorInitRequiresZeroInit = false;
318920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  for (step_iterator Step = step_begin(), StepEnd = step_end();
319020093b4bf698f292c664676987541d5103b65b15Douglas Gregor       Step != StepEnd; ++Step) {
319120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (CurInit.isInvalid())
319220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return S.ExprError();
319320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
319420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Expr *CurInitExpr = (Expr *)CurInit.get();
319599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType();
319620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
319720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    switch (Step->Kind) {
319820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_ResolveAddressOfOverloadedFunction:
319920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Overload resolution determined which function invoke; update the
320020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // initializer to reflect that choice.
320120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit = S.FixOverloadedFunctionReference(move(CurInit), Step->Function);
320220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
320320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
320420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_CastDerivedToBaseRValue:
320520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_CastDerivedToBaseLValue: {
320620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // We have a derived-to-base cast that produces either an rvalue or an
320720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // lvalue. Perform that cast.
320820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
320920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Casts to inaccessible base classes are allowed with C-style casts.
321020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
321120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
321220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         CurInitExpr->getLocStart(),
321320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         CurInitExpr->getSourceRange(),
321420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         IgnoreBaseAccess))
321520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return S.ExprError();
321620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
321720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit = S.Owned(new (S.Context) ImplicitCastExpr(Step->Type,
321820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                    CastExpr::CK_DerivedToBase,
321920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                      (Expr*)CurInit.release(),
322020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     Step->Kind == SK_CastDerivedToBaseLValue));
322120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
322220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
322320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
322420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_BindReference:
322520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (FieldDecl *BitField = CurInitExpr->getBitField()) {
322620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // References cannot bind to bit fields (C++ [dcl.init.ref]p5).
322720093b4bf698f292c664676987541d5103b65b15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
3228d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor          << Entity.getType().isVolatileQualified()
322920093b4bf698f292c664676987541d5103b65b15Douglas Gregor          << BitField->getDeclName()
323020093b4bf698f292c664676987541d5103b65b15Douglas Gregor          << CurInitExpr->getSourceRange();
323120093b4bf698f292c664676987541d5103b65b15Douglas Gregor        S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
323220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return S.ExprError();
323320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
323420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
323520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Reference binding does not have any corresponding ASTs.
323620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
323720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Check exception specifications
323820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
323920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return S.ExprError();
324020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
324120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
324220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_BindReferenceToTemporary:
324320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Check exception specifications
324420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
324520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return S.ExprError();
324620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
324720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // FIXME: At present, we have no AST to describe when we need to make a
324820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // temporary to bind a reference to. We should.
324920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
325020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
325120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_UserConversion: {
325220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // We have a user-defined conversion that invokes either a constructor
325320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // or a conversion function.
325420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CastExpr::CastKind CastKind = CastExpr::CK_Unknown;
325518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      bool IsCopy = false;
325620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (CXXConstructorDecl *Constructor
325720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              = dyn_cast<CXXConstructorDecl>(Step->Function)) {
325820093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build a call to the selected constructor.
325920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S);
326020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        SourceLocation Loc = CurInitExpr->getLocStart();
326120093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CurInit.release(); // Ownership transferred into MultiExprArg, below.
326220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
326320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Determine the arguments required to actually perform the constructor
326420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // call.
326520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (S.CompleteConstructorCall(Constructor,
326620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      Sema::MultiExprArg(S,
326720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                         (void **)&CurInitExpr,
326820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                         1),
326920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      Loc, ConstructorArgs))
327020093b4bf698f292c664676987541d5103b65b15Douglas Gregor          return S.ExprError();
327120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
327220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build the an expression that constructs a temporary.
327320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
327420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                          move_arg(ConstructorArgs));
327520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (CurInit.isInvalid())
327620093b4bf698f292c664676987541d5103b65b15Douglas Gregor          return S.ExprError();
327720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
327820093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CastKind = CastExpr::CK_ConstructorConversion;
327918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
328018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
328118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor            S.IsDerivedFrom(SourceType, Class))
328218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          IsCopy = true;
328320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      } else {
328420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build a call to the conversion function.
328520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Step->Function);
328618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
328720093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // FIXME: Should we move this initialization into a separate
328820093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // derived-to-base conversion? I believe the answer is "no", because
328920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // we don't want to turn off access control here for c-style casts.
329020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (S.PerformObjectArgumentInitialization(CurInitExpr, Conversion))
329120093b4bf698f292c664676987541d5103b65b15Douglas Gregor          return S.ExprError();
329220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
329320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Do a little dance to make sure that CurInit has the proper
329420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // pointer.
329520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CurInit.release();
329620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
329720093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build the actual call to the conversion function.
329820093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CurInit = S.Owned(S.BuildCXXMemberCallExpr(CurInitExpr, Conversion));
329920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (CurInit.isInvalid() || !CurInit.get())
330020093b4bf698f292c664676987541d5103b65b15Douglas Gregor          return S.ExprError();
330120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
330220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CastKind = CastExpr::CK_UserDefinedConversion;
330320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
330420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
330518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (shouldBindAsTemporary(Entity, IsCopy))
330618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
330718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
330820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInitExpr = CurInit.takeAs<Expr>();
330920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit = S.Owned(new (S.Context) ImplicitCastExpr(CurInitExpr->getType(),
331020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                         CastKind,
331120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                         CurInitExpr,
331218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                                         false));
331318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
331418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (!IsCopy)
33157abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor        CurInit = CopyIfRequiredForEntity(S, Entity, Kind, move(CurInit));
331620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
331720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
331820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
331920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_QualificationConversionLValue:
332020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_QualificationConversionRValue:
332120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Perform a qualification conversion; these can never go wrong.
332220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      S.ImpCastExprToType(CurInitExpr, Step->Type,
332320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                          CastExpr::CK_NoOp,
332420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                          Step->Kind == SK_QualificationConversionLValue);
332520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit.release();
332620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit = S.Owned(CurInitExpr);
332720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
332820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
332920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_ConversionSequence:
33306864748fc9a780e6db0bb5a7bd20aa889882dc94Douglas Gregor        if (S.PerformImplicitConversion(CurInitExpr, Step->Type, Sema::AA_Converting,
333120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      false, false, *Step->ICS))
333220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return S.ExprError();
333320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
333420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit.release();
333520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CurInit = S.Owned(CurInitExpr);
333620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
3337d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3338d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    case SK_ListInitialization: {
3339d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      InitListExpr *InitList = cast<InitListExpr>(CurInitExpr);
3340d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      QualType Ty = Step->Type;
3341cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty))
3342d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        return S.ExprError();
3343d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3344d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      CurInit.release();
3345d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      CurInit = S.Owned(InitList);
3346d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      break;
3347d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    }
334851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
334951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    case SK_ConstructorInitialization: {
335051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      CXXConstructorDecl *Constructor
335151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        = cast<CXXConstructorDecl>(Step->Function);
335251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
335351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      // Build a call to the selected constructor.
335451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S);
335551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      SourceLocation Loc = Kind.getLocation();
335651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
335751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      // Determine the arguments required to actually perform the constructor
335851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      // call.
335951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      if (S.CompleteConstructorCall(Constructor, move(Args),
336051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                    Loc, ConstructorArgs))
336151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        return S.ExprError();
336251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
336351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      // Build the an expression that constructs a temporary.
3364d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor      CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
3365745880f35066bdb1950d0e870608295221346fc5Douglas Gregor                                        Constructor,
336616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor                                        move_arg(ConstructorArgs),
336716006c901315fa12a108b4e571f187f4b676e426Douglas Gregor                                        ConstructorInitRequiresZeroInit);
336851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      if (CurInit.isInvalid())
336951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        return S.ExprError();
337018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
337118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      bool Elidable
337218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        = cast<CXXConstructExpr>((Expr *)CurInit.get())->isElidable();
337318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (shouldBindAsTemporary(Entity, Elidable))
337418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
337518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
337618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (!Elidable)
33777abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor        CurInit = CopyIfRequiredForEntity(S, Entity, Kind, move(CurInit));
337851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      break;
337951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    }
338071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
338171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    case SK_ZeroInitialization: {
338216006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      step_iterator NextStep = Step;
338316006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      ++NextStep;
338416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      if (NextStep != StepEnd &&
338516006c901315fa12a108b4e571f187f4b676e426Douglas Gregor          NextStep->Kind == SK_ConstructorInitialization) {
338616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        // The need for zero-initialization is recorded directly into
338716006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        // the call to the object's constructor within the next step.
338816006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        ConstructorInitRequiresZeroInit = true;
338916006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      } else if (Kind.getKind() == InitializationKind::IK_Value &&
339016006c901315fa12a108b4e571f187f4b676e426Douglas Gregor                 S.getLangOptions().CPlusPlus &&
339116006c901315fa12a108b4e571f187f4b676e426Douglas Gregor                 !Kind.isImplicitValueInit()) {
339271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor        CurInit = S.Owned(new (S.Context) CXXZeroInitValueExpr(Step->Type,
339371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                                   Kind.getRange().getBegin(),
339471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                                    Kind.getRange().getEnd()));
339516006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      } else {
339671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor        CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type));
339716006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      }
339871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      break;
339971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    }
340018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
340118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    case SK_CAssignment: {
340218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      QualType SourceType = CurInitExpr->getType();
340318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      Sema::AssignConvertType ConvTy =
340418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr);
3405aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor
3406aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor      // If this is a call, allow conversion to a transparent union.
3407aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor      if (ConvTy != Sema::Compatible &&
3408aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor          Entity.getKind() == InitializedEntity::EK_Parameter &&
3409aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor          S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr)
3410aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor            == Sema::Compatible)
3411aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor        ConvTy = Sema::Compatible;
3412aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor
341318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
341418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                     Step->Type, SourceType,
341518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                     CurInitExpr, getAssignmentAction(Entity)))
341618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        return S.ExprError();
341718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
341818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      CurInit.release();
341918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      CurInit = S.Owned(CurInitExpr);
342018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      break;
342118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    }
3422cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
3423cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    case SK_StringInit: {
3424cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman      QualType Ty = Step->Type;
3425cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman      CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, S);
3426cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman      break;
3427cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    }
342820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
342920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
343020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
343120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return move(CurInit);
343220093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
343320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
343420093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
343520093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Diagnose initialization failures
343620093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
343720093b4bf698f292c664676987541d5103b65b15Douglas Gregorbool InitializationSequence::Diagnose(Sema &S,
343820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      const InitializedEntity &Entity,
343920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      const InitializationKind &Kind,
344020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      Expr **Args, unsigned NumArgs) {
344120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (SequenceKind != FailedSequence)
344220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return false;
344320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
3444d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
344520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  switch (Failure) {
344620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_TooManyInitsForReference:
344720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
344820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
344920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
345020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
345120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ArrayNeedsInitList:
345220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ArrayNeedsInitListOrStringLiteral:
345320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list)
345420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << (Failure == FK_ArrayNeedsInitListOrStringLiteral);
345520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
345620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
345720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_AddressOfOverloadFailed:
345820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.ResolveAddressOfOverloadedFunction(Args[0],
345920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         DestType.getNonReferenceType(),
346020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         true);
346120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
346220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
346320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ReferenceInitOverloadFailed:
34644a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  case FK_UserConversionOverloadFailed:
346520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    switch (FailedOverloadResult) {
346620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Ambiguous:
346718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (Failure == FK_UserConversionOverloadFailed)
346818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
346918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << Args[0]->getType() << DestType
347018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << Args[0]->getSourceRange();
347118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      else
347218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
347318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << DestType << Args[0]->getType()
347418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << Args[0]->getSourceRange();
347518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
3476cbce60633c9864261105b289473e5a3ed7b4a729John McCall      S.PrintOverloadCandidates(FailedCandidateSet, Sema::OCD_ViableCandidates,
3477cbce60633c9864261105b289473e5a3ed7b4a729John McCall                                Args, NumArgs);
347820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
347920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
348020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_No_Viable_Function:
348120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
348220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getType() << DestType.getNonReferenceType()
348320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getSourceRange();
3484cbce60633c9864261105b289473e5a3ed7b4a729John McCall      S.PrintOverloadCandidates(FailedCandidateSet, Sema::OCD_AllCandidates,
3485cbce60633c9864261105b289473e5a3ed7b4a729John McCall                                Args, NumArgs);
348620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
348720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
348820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Deleted: {
348920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
349020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getType() << DestType.getNonReferenceType()
349120093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getSourceRange();
349220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      OverloadCandidateSet::iterator Best;
349320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet,
349420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                   Kind.getLocation(),
349520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                   Best);
349620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (Ovl == OR_Deleted) {
349720093b4bf698f292c664676987541d5103b65b15Douglas Gregor        S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
349820093b4bf698f292c664676987541d5103b65b15Douglas Gregor          << Best->Function->isDeleted();
349920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      } else {
35009f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin        llvm_unreachable("Inconsistent overload resolution?");
350120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
350220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
350320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
350420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
350520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Success:
35069f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("Conversion did not fail!");
350720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
350820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
350920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
351020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
351120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_NonConstLValueReferenceBindingToTemporary:
351220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_NonConstLValueReferenceBindingToUnrelated:
351320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(),
351420093b4bf698f292c664676987541d5103b65b15Douglas Gregor           Failure == FK_NonConstLValueReferenceBindingToTemporary
351520093b4bf698f292c664676987541d5103b65b15Douglas Gregor             ? diag::err_lvalue_reference_bind_to_temporary
351620093b4bf698f292c664676987541d5103b65b15Douglas Gregor             : diag::err_lvalue_reference_bind_to_unrelated)
351720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType.getNonReferenceType()
351820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
351920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
352020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
352120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
352220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_RValueReferenceBindingToLValue:
352320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
352420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
352520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
352620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
352720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ReferenceInitDropsQualifiers:
352820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
352920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType.getNonReferenceType()
353020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
353120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
353220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
353320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
353420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ReferenceInitFailed:
353520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
353620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType.getNonReferenceType()
353720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid)
353820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
353920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
354020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
354120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
354220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ConversionFailed:
354318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Kind.getLocation(), diag::err_init_conversion_failed)
354418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << (int)Entity.getKind()
354520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType
354620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid)
354720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
354820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
3549d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
3550d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3551d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case FK_TooManyInitsForScalar: {
355299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    SourceRange R;
355399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
355499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
355599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      R = SourceRange(InitList->getInit(1)->getLocStart(),
355699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                      InitList->getLocEnd());
355799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    else
355899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      R = SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
3559d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3560d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_excess_initializers)
356199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      << /*scalar=*/2 << R;
3562d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
3563d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  }
3564d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3565d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case FK_ReferenceBindingToInitList:
3566d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
3567d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      << DestType.getNonReferenceType() << Args[0]->getSourceRange();
3568d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
3569d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
3570d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case FK_InitListBadDestinationType:
3571d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
3572d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
3573d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
357451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
357551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  case FK_ConstructorOverloadFailed: {
357651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    SourceRange ArgsRange;
357751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    if (NumArgs)
357851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      ArgsRange = SourceRange(Args[0]->getLocStart(),
357951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                              Args[NumArgs - 1]->getLocEnd());
358051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
358151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    // FIXME: Using "DestType" for the entity we're printing is probably
358251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    // bad.
358351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    switch (FailedOverloadResult) {
358451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_Ambiguous:
358551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
358651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          << DestType << ArgsRange;
358781201626aa08bcc9d05c8b3c6a1d38a7d577c3ceJohn McCall        S.PrintOverloadCandidates(FailedCandidateSet,
3588cbce60633c9864261105b289473e5a3ed7b4a729John McCall                                  Sema::OCD_ViableCandidates, Args, NumArgs);
358951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
359051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
359151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_No_Viable_Function:
359251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
359351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          << DestType << ArgsRange;
3594cbce60633c9864261105b289473e5a3ed7b4a729John McCall        S.PrintOverloadCandidates(FailedCandidateSet, Sema::OCD_AllCandidates,
3595cbce60633c9864261105b289473e5a3ed7b4a729John McCall                                  Args, NumArgs);
359651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
359751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
359851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_Deleted: {
359951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
360051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          << true << DestType << ArgsRange;
360151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        OverloadCandidateSet::iterator Best;
360251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet,
360351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                                     Kind.getLocation(),
360451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                                     Best);
360551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        if (Ovl == OR_Deleted) {
360651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
360751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor            << Best->Function->isDeleted();
360851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        } else {
360951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          llvm_unreachable("Inconsistent overload resolution?");
361051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        }
361151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
361251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      }
361351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
361451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_Success:
361551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        llvm_unreachable("Conversion did not fail!");
361651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
361751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    }
361851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    break;
361951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  }
362099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
362199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case FK_DefaultInitOfConst:
362299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    S.Diag(Kind.getLocation(), diag::err_default_init_const)
362399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      << DestType;
362499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    break;
362520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
362620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
362720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return true;
362820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
362918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
363018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor//===----------------------------------------------------------------------===//
363118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor// Initialization helper functions
363218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor//===----------------------------------------------------------------------===//
363318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas GregorSema::OwningExprResult
363418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas GregorSema::PerformCopyInitialization(const InitializedEntity &Entity,
363518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                SourceLocation EqualLoc,
363618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                OwningExprResult Init) {
363718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (Init.isInvalid())
363818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return ExprError();
363918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
364018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Expr *InitE = (Expr *)Init.get();
364118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  assert(InitE && "No initialization expression?");
364218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
364318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (EqualLoc.isInvalid())
364418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    EqualLoc = InitE->getLocStart();
364518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
364618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
364718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                                           EqualLoc);
364818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
364918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Init.release();
365018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  return Seq.Perform(*this, Entity, Kind,
365118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                     MultiExprArg(*this, (void**)&InitE, 1));
365218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
3653